users@jersey.java.net

[Jersey] Re: Ignore unknown JSON properties

From: Tatu Saloranta <tsaloranta_at_gmail.com>
Date: Mon, 28 Mar 2011 22:07:37 -0700

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 +-