简体   繁体   中英

How to tell jackson to ignore some fields when deserialising

I am calling an endpoint in which it returns an object. In this object it contains some fields and also a field of another type of object. Eg

class ResponseObject{
private final boolean success;
private final String message;    
private final DifferentType different;
}

I am calling the endpoint via RestTemplate:

   private LogonResponseMessage isMemberAuthenticated(UserCredentialDomain userCredentialDomain)
   {
      RestTemplate restTemplate = new RestTemplate();
      return restTemplate.getForObject(
         "http://abc.local:8145/xyz/member/authenticate/{memberLoginName}/{password}", ResponseObject.class,
         userCredentialDomain.getUsername(), userCredentialDomain.getPassword());
   }

So my consuming app is giving this error:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `fgh.thg.member.DifferentTypeABC` (no Creators, like default constructor, exist): cannot deserialize from Object v
alue (no delegate- or property-based Creator)

I am aware that it's telling me to put a default constructor in the DifferentTypeABC class in order for jackson to deserialise it but i can't do that easily as I'll need to update the dependency on apps that the DifferentTypeABC is in across several different repos.

So i was wondering if there is a way of configuring RestTemplate or jackson on the consuming app so that it ignores attempting to deserialise objects if it doesn't contain a default constructor? To be honest, I am pretty much interested in the success and message fields on the response object.

Another way to solve the problem is by enhancing DifferentType in this module using Jackson creator Mixin . Below is an example:

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

import java.io.IOException;

public class Test {

  public static void main(String[] args) throws IOException {
    ObjectMapper mapper = new ObjectMapper();

    mapper.setVisibility(mapper.getVisibilityChecker()
        .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
        .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
        .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
        .withCreatorVisibility(JsonAutoDetect.Visibility.NONE));

    mapper.addMixIn(DifferentType.class, DifferentTypeMixin.class);
    String raw = "{\"message\": \"ok\", \"differentType\": {\"name\": \"foo bar\"}}";
    ResponseObject object = mapper.readValue(raw, ResponseObject.class);
    System.out.println(mapper.writeValueAsString(object));
  }

  @Data
  static class ResponseObject {
    private String message;
    private DifferentType differentType;
  }

  static class DifferentType {
    private String name;

    public DifferentType(String name) {
      this.name = name;
    }
  }

  public static abstract class DifferentTypeMixin {
    @JsonCreator
    DifferentTypeMixin(@JsonProperty("name") String name) {
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM