Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 838
Daniel Kaplan Avatar asked Dec 15 '22 02:12

Daniel Kaplan


1 Answers

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?

like image 68
Brian Roach Avatar answered Feb 23 '23 01:02

Brian Roach