How do I tell the Jersey Client to do the equivalent of @JsonIgnoreProperties(ignoreUnknown = true) on every class it deserializes?

StackOverflow https://stackoverflow.com/questions/21462425

Question

I have a class that I'm using the Jersey Client to deserialize. This class has a method that looks like this:

public boolean isEmpty() {
    return (code == null &&
            label == null &&
            codeSystem == null &&
            codeSystemLabel == null &&
            description == null &&
            concept == null &&
            alternateCode == null
    );

There is no setter. As-is, this will throw this exception:

com.sun.jersey.api.client.ClientHandlerException: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "empty" (Class com.app.models.CodedElementModel), not marked as ignorable
 at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@3b927d51; line: 1, column: 270] (through reference chain: com.app.models.LabOrderModel["code"]->com.app.models.CodedElementModel["empty"])

I've read this article, and it turns out I can fix this by putting this annotation on the CodedElementModel class: @JsonIgnoreProperties(ignoreUnknown = true).

The problem is I have a lot of methods throwing this exception. Is there a way to configure the Jersey Client to act as if @JsonIgnoreProperties(ignoreUnknown = true) is set on every class so I don't have to annotate them by hand? I don't want to have to change ~30 files by adding this annotation manually. This will prevent such errors in the future if someone adds a getter without a setter.

Here's how I'm creating my Jersey Client:

    DefaultClientConfig clientConfig = new DefaultClientConfig();
    clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
    Client client = Client.create(clientConfig);

I feel like that clientConfig may have a setting to do this, but I'm not sure how to find it.

Was it helpful?

Solution

You would need to configure this by instantiating and configuring the JacksonJsonProvider for the Jersey Client.

Jersey V2.x:

JacksonJsonProvider jacksonJsonProvider = 
    new JacksonJaxbJsonProvider()
        .configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

client.register(jacksonJsonProvider);

Jersey V1.x:

DefaultClientConfig clientConfig = new DefaultClientConfig();
JacksonJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider().
    configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
clientConfig.getSingletons().add(jacksonJsonProvider);

Worth noting is that the underlying issue is that your JSON contains the field "empty" ... I'm assuming it's getting there because you're serializing that same POJO. Wouldn't annotating that isEmpty() method to be ignored for serialization if you actually don't want it in the JSON be better?

OTHER TIPS

Accepted response from @sleske, didn't work for me out of the box as I'm using com.sun.jersey.api.client.Client and com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider. What actually worked for me is this ,

ClientConfig config = new DefaultClientConfig();

JacksonJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
config.getSingletons().add(jacksonJsonProvider);
Client client = Client.create(config);

Cheers!

You can also construct your custom ObjectMapper and pass it in using this.

ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
final JacksonJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider(mapper, JacksonJaxbJsonProvider.DEFAULT_ANNOTATIONS).configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

final Client    client  = ClientBuilder.newClient().register(jacksonJsonProvider);

With this maven dependency

    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.6.6</version>
    </dependency> 
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.22.1</version>
    </dependency> 

As documented here https://jersey.java.net/documentation/latest/migration.html#mig-1-x-json

Using REST service on Client end I ran into the same problem. Here is my fix:

1) Add com.fasterxml.jackson.jaxrs to your POM or include the jar in your project.

2) Modify the ClientConfig:

JacksonJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider();
jacksonJsonProvider.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
clientConfig.getClasses().add(jacksonJsonProvider.getClass());

Client client = Client.create(clientConfig);        
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top