Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

java - 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. E.g.

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.

question from:https://stackoverflow.com/questions/65832158/how-to-tell-jackson-to-ignore-some-fields-when-deserialising

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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) {
    }
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...