How can I ignore unrecognized properties using javax.ws.rs.client.Invocation.Builder? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How can I ignore unrecognized properties using javax.ws.rs.client.Invocation.Builder? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have built a web client which uses SSL.

The client is initialized like this:

SSLContext sc = SSLContext.getInstance("ssl");
        sc.init(null, noopTrustManager, null);
        
        this.client = ClientBuilder.newBuilder().
                sslContext(sc).
                build();

Then the web target is initialized like this:

this.webTarget = this.client.target(urlAddress);

And the Invocation Builder is initialized like this:

Builder request = this.webTarget.request();

But when – after setting the headers appropriately – I try to do this:

request.get(InitiateTransferResponse.class);

I get the following error message:

javax.ws.rs.client.ResponseProcessingException: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field “timestamp” (class com.interaxa.ivr.visual.moa.service.model.InitiateTransferResponse), not marked as ignorable

Is there a way to tell the builder to ignore all unrecognized properties? (I intend to use this same builder for other web services too, and it would be nice if I could set it to ignore unknown properties permanently, regardless of the response class).

Thank you in advance.

Answer

I have found a solution!

The key is to replace the line: request.get(InitiateTransferResponse.class);

By this code:

        InitiateTransferResponse result;
        try {
            result = getMapper().readValue(response, InitiateTransferResponse.class);
        } 
        catch (IOException e) {
            ViewFactory.logError(e);
            result = null;
        }

Where getMapper() is as follows:

public ObjectMapper getMapper() {
        if (mapper == null){
            mapper = new ObjectMapper();
            mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            mapper.configure(DeserializationConfig.Feature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
            mapper.setSerializationInclusion(Inclusion.NON_NULL);
        }
        return mapper;
}

And ObjectMapper is org.codehaus.jackson.map.ObjectMapper.

This way, the ObjectMapper controls what it lets through rather than leaving that decision to the Builder.

We are here to answer your question about How can I ignore unrecognized properties using javax.ws.rs.client.Invocation.Builder? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji