users@jersey.java.net

[Jersey] Re: Ignore unknown JSON properties

From: Tauren Mills <tauren_at_groovee.com>
Date: Wed, 30 Mar 2011 15:48:37 -0700

Tatu,

This is exactly what I was looking for! It would be nice to configure this
on a per-pojo basis with some sort of annotation in the pojo. Right now its
an all-or-nothing solution.

I can't seem to determine how my JacksonMapperProvider is getting used, can
you help? I don't have a problem, but I implemented it a long time ago and
can't remember how it all works.

What tells my resources to use the ObjectMapper I configure in
JacksonMapperProvider? I'm assuming it due to Spring @Component
auto-detection, but I'm hoping to learn more specifics about it. Does using
Jersey with Spring automatically search for an instance of ContextResolver
to use?

Code and config provided below.

Thanks,
Tauren

----
I have this in my Spring config:
<context:component-scan base-package="com.company.rest.jersey"/>
Here's my JacksonMapperProvider:
package com.company.rest.jersey;
@Provider
@Component
@Produces({MediaType.APPLICATION_JSON})
public class JacksonMapperProvider implements ContextResolver<ObjectMapper>
{
   ObjectMapper mapper;
   public JacksonMapperProvider(){
       mapper = new ObjectMapper();
       mapper.configure(Feature.INDENT_OUTPUT, true);
       // Serialize dates using ISO8601 format
       // Jackson uses timestamps by default, so use StdDateFormat to get
ISO8601
       mapper.getSerializationConfig().setDateFormat(new StdDateFormat());
       // Deserialize dates using ISO8601 format
       // MilliDateFormat simply adds milliseconds to string if missing so
it will parse
       mapper.getDeserializationConfig().setDateFormat(new
MilliDateFormat());
       // Prevent exceptions from being thrown for unknown properties
       mapper.configure(
  DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,false);
   }
   @Override
   public ObjectMapper getContext(Class<?> aClass) {
       return mapper;
   }
}
Here's my Application:
package com.company.rest.jersey;
public final class RestApplication extends Application
{
    public Set<Object> getSingletons() {
        HashSet<Object> singletons = new HashSet<Object>();
        singletons.add(new JacksonJsonProvider());
        return singletons;
    }
}
And part of my web.xml:
<servlet> <servlet-name>Jersey Spring</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param> <param-name>javax.ws.rs.Application</param-name>
<param-value>com.company.rest.jersey.RestApplication</param-value>
</init-param> <load-on-startup>1</load-on-startup> </servlet>
<servlet-mapping>
  <servlet-name>Jersey Spring</servlet-name>
  <url-pattern>/services/*</url-pattern>
</servlet-mapping>
On Mon, Mar 28, 2011 at 10:07 PM, Tatu Saloranta <tsaloranta_at_gmail.com>wrote:
> On Mon, Mar 28, 2011 at 9:58 PM, Tauren Mills <tauren_at_groovee.com> wrote:
> > Is there a way to prevent an exception from being thrown if there are
> extra
> > properties in the JSON data sent to a resource? If there are any
> properties
> > in the JSON that do not map to the POJO, and exception gets thrown. I
> want
> > to just ignore those properties.
> > @Consumes(MediaType.APPLICATION_JSON)
> > public Response createItem(ItemDTO dto) {
> >     ...
> > }
> > public class ItemDTO {
> >    String name;
> > }
> > JSON:
> > {name: "Item 1", junk: "Property not in POJO"}
> > An exception is thrown because ItemDTO.junk doesn't exist. How can I
> ignore
> > extra properties?
>
> With Jackson, easiest way is to configure ObjectMapper like so:
>
>
>  objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES,
> false);
>
> Since this is such a commonly needed thing, perhaps there may be a
> pojo configuration shortcut; or if not, maybe worth adding it to
> jersey?
>
> -+ Tatu +-
>