Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey 2.8 client is not ignoring unknown properties during deserialization

I am using Jersey Client 2.8 and trying to register my own Jackson configurator which will sets custom ObjectMapper properties.

public class ConnectionFactory {
   private final Client client;
   public ConnectionFactory() {
   ClientConfig clientConfig = new ClientConfig();
   clientConfig.property(ClientProperties.FOLLOW_REDIRECTS, true);
   clientConfig.property(ClientProperties.CONNECT_TIMEOUT, connTimeoutSec * 1000);
   clientConfig.property(ClientProperties.READ_TIMEOUT, readTimeoutSec * 1000);
   this.client = ClientBuilder.newBuilder().register(JacksonConfigurator.class).register(JacksonFeature.class).withConfig(clientConfig).build();
   // Some more code here ...
   }
}

According to Example 8.15 in Jackson Registration documentation, this should register the following JacksonConfigurator class to the client.

import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Provides custom configuration for jackson.
 */
@Provider
public class JacksonConfigurator implements ContextResolver<ObjectMapper> {

    private final ObjectMapper mapper;

    public JacksonConfigurator() {
        mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    @Override
    public ObjectMapper getContext(Class<?> type) {
        return mapper;
    }

}

If I deserialize the response from client, the client should ignore unrecognized fields in the response. But I am getting following error -

Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "xyz" (class LookupData), not marked as ignorable (3 known properties: "abc", "pqr", "def"])
 at [Source: org.glassfish.jersey.message.internal.EntityInputStream@55958273; line: 1, column: 11] (through reference chain: LookupData["xyz"])
    at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:51)
    at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:731)
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:915)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1292)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1270)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:247)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118)
    at com.fasterxml.jackson.databind.ObjectReader._bind(ObjectReader.java:1232)
    at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:676)
    at com.fasterxml.jackson.jaxrs.base.ProviderBase.readFrom(ProviderBase.java:800)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.invokeReadFrom(ReaderInterceptorExecutor.java:257)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:229)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:149)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1124)
    at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:851)
    ... 39 more

Can someone please let me know if I am missing something while registering the JacksonConfigurator class?

like image 535
Onkar Deshpande Avatar asked Oct 15 '14 22:10

Onkar Deshpande


People also ask

How to ignore unknown properties in JSON?

If a Model class is being created to represent the JSON in Java, then the class can be annotated as @JsonIgnoreProperties(ignoreUnknown = true) to ignore any unknown field.

How do you tell Jackson to ignore a field during Deserialization?

Overview So when Jackson is reading from JSON string, it will read the property and put into the target object. But when Jackson attempts to serialize the object, it will ignore the property. For this purpose, we'll use @JsonIgnore and @JsonIgnoreProperties.

How to ignore unknown properties while parsing xml in Java?

Ignoring unknown properties using @JsonIgnoreProperties If you are creating a Model class to represent the JSON in Java, then you can annotate the class with @JsonIgnoreProperties(ignoreUnknown = true) to ignore any unknown field.

What is Fail_on_unknown_properties?

FAIL_ON_UNKNOWN_PROPERTIES. Feature that determines whether encountering of unknown properties (ones that do not map to a property, and there is no "any setter" or handler that can handle it) should result in a failure (by throwing a JsonMappingException ) or not.


2 Answers

Try initializing the Jersey client with a JacksonJsonProvider that's been configured appropriately:

final JacksonJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
final Client client = ClientBuilder.newClient(new ClientConfig(jacksonJsonProvider));

This was tested with Jackson 2.5.1 and Jersey 2.17

like image 152
Scott Kidder Avatar answered Oct 23 '22 09:10

Scott Kidder


I had same issue and looking for a solution... find your proposition and it works for me :)

My test is very basic / see here below :

    Client client = ClientBuilder.newClient()
            .register(JacksonFeature.class).register(JacksonConfigurator.class);


    WebTarget wt = client.target(REST_SERVICE_URL).path(
            "amount");
    wt = wt.queryParam("from", "USD");

    ConverterResponse cr=wt.request().get(ConverterResponse.class);

If I don't register your JacksonConfigurator, I get same exception as yours :

UnrecognizedPropertyException: Unrecognized field 

Because converterResponse defines a subset of REST response object methods.

Thank you very much !

like image 34
skay Avatar answered Oct 23 '22 10:10

skay