7

I am using Jersey 1.0 http-client to call a resource and deserialize the response JSON like this:

Client client = Client.create(new DefaultClientConfig())
ClientResponse clientResponse = client.resource("http://some-uri").get(ClientResponse.class)
MyJsonRepresentingPOJO pojo = clientResponse.getEntity(MyJsonRepresentingPOJO.class)

Now the response JSON has some new fields and I am getting following exception:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "xyz"

How can I change jackson's deserialization-mode to NON-STRICT, so that it will ignore the new fields?

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
woezelmann
  • 1,355
  • 2
  • 19
  • 39

1 Answers1

13

To configure the ObjectMapper for use with Jersey, you could

  1. Create a ContextResolver as seen here, and register the resolver with the client.

    ClientConfig config = new DefaultClientConfig();
    config.register(new ObjectMapperContextResolver());
    Client client = Client.create(config);
    
  2. OR Instantiate the JacksonJsonProvider passing in the ObjectMapper as a constructor argument. Then register the provider with the Client

    ClientConfig config = new DefaultClientConfig();
    config.register(new JacksonJsonProvider(mapper));
    Client client = Client.create(config);
    

    Note, if you are using JAXB annotations, you'll want to use the JacksonJaxbJsonProvider

To ignore the unknown properties, you can you set a configuration property on the ObjectMapper, as shown in the link from Sam B.. i.e

mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

EDIT

I made a mistake in the examples above. There is no register method for the ClientConfig in Jersey 1.x. Instead, use getSingletons().add(...). See the API for more info.

Michael Dussere
  • 498
  • 7
  • 25
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • 1
    The ClientConfig class in the jersey-client-1.18.jar does not have a register() method... – woezelmann Jul 09 '15 at 10:03
  • 1
    Sorry, I'm so used to using Jersey 2.x. See the [Javadoc](https://jersey.java.net/apidocs/1.18/jersey/com/sun/jersey/api/client/config/ClientConfig.html). You should be able to use `getSingletons().add(...)` – Paul Samsotha Jul 09 '15 at 10:08
  • 1
    from the above link to save you some clicking - in Jersey 1.x DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES is DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES – hello_earth Mar 04 '22 at 09:51