How do I tell the Jersey client the equivalent of @JsonIgnoreProperties (ignoreUnknown = true) for each class that it deserializes?

I have a class in which I use 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
    );

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 read this article , and it turns out I can fix it by putting this annotation to the class CodedElementModel: @JsonIgnoreProperties(ignoreUnknown = true).

, , . , , @JsonIgnoreProperties(ignoreUnknown = true) , ? ~ 30 , . , - .

-:

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

, clientConfig , , .

+4
4

, JacksonJsonProvider Client.

V2.x:

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

client.register(jacksonJsonProvider);

V1.x:

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

, , JSON ""... , , POJO. , isEmpty() , , JSON ?

+6

@sleske, , com.sun.jersey.api.client.Client com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider. ,

ClientConfig config = new DefaultClientConfig();

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

!

+4

ObjectMapper .

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);

maven

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

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

+1

Using the client side REST service, I ran into the same problem. Here is my fix:

1) Add com.fasterxml.jackson.jaxrsto your POM or include a jar in your project.

2) Change 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);        
-1
source

Source: https://habr.com/ru/post/1524547/


All Articles