Jackson JSON deserialization with multiple parameters constructor

前端 未结 3 1534
無奈伤痛
無奈伤痛 2020-12-10 01:05

I\'ve been using FasterXML/Jackson-Databind in my project for a while now, and all was working great, until I\'ve discovered this post and started to use this approach to de

相关标签:
3条回答
  • 2020-12-10 01:29

    @JsonCreator is not required after having @JsonProperty("xxx") in the parameter

    0 讨论(0)
  • 2020-12-10 01:44

    Short answer: use Java 8, javac -parameters, and jackson-module-parameter-names

    Long answer: Why when a constructor is annotated with @JsonCreator, its arguments must be annotated with @JsonProperty?

    0 讨论(0)
  • 2020-12-10 01:45

    You need to add the annotation @JsonProperty specifying the name of the json property that needs to be passed to the constructor when creating the object.

    public class Cruise extends WaterVehicle {
    
     private Integer maxSpeed;
    
      @JsonCreator
      public Cruise(@JsonProperty("name") String name, @JsonProperty("maxSpeed")Integer maxSpeed) {
        super(name);
        System.out.println("Cruise.Cruise");
        this.maxSpeed = maxSpeed;
      }
    
      public Integer getMaxSpeed() {
        return maxSpeed;
      }
    
      public void setMaxSpeed(Integer maxSpeed) {
        this.maxSpeed = maxSpeed;
      }
    
    }
    

    EDIT

    I just tested using the below code and it works for me

    import java.io.IOException;
    
    import com.fasterxml.jackson.annotation.JsonCreator.Mode;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.SerializationFeature;
    import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
    
    class WaterVehicle {
    
        private String name;
        private int capacity;
        private String inventor;
        public WaterVehicle(String name) {
            this.name=name;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getCapacity() {
            return capacity;
        }
        public void setCapacity(int capacity) {
            this.capacity = capacity;
        }
        public String getInventor() {
            return inventor;
        }
        public void setInventor(String inventor) {
            this.inventor = inventor;
        }
    
    
    }
    
     class Cruise  extends WaterVehicle{
    
            private Integer maxSpeed;
    
            public Cruise(String name, Integer maxSpeed) {
                super(name);
                this.maxSpeed = maxSpeed;
            }
    
            public Integer getMaxSpeed() {
                return maxSpeed;
            }
    
            public void setMaxSpeed(Integer maxSpeed) {
                this.maxSpeed = maxSpeed;
            }
    
    
        }
    
    public class Test {
          public static void main(String[] args) throws IOException {
            Cruise cruise = new Cruise("asd", 100);
            cruise.setMaxSpeed(100);
            cruise.setCapacity(123);
            cruise.setInventor("afoaisf");
    
            ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
            mapper.registerModule(new ParameterNamesModule(Mode.PROPERTIES));
    
            String jsonString = mapper.writeValueAsString( cruise);
            System.out.println(jsonString);
    
            Cruise anotherCruise = mapper.readValue(jsonString, Cruise.class);
             System.out.println(anotherCruise );
             jsonString = mapper.writeValueAsString( anotherCruise );
             System.out.println(jsonString);
    
        }
    
    }
    

    It produces the following output

    {
      "name" : "asd",
      "capacity" : 123,
      "inventor" : "afoaisf",
      "maxSpeed" : 100
    }
    Cruise@56f4468b
    {
      "name" : "asd",
      "capacity" : 123,
      "inventor" : "afoaisf",
      "maxSpeed" : 100
    }
    

    Make sure you have the compilerArgs in the pom file.

    <compilerArgs>
         <arg>-parameters</arg>
    </compilerArgs>
    
    0 讨论(0)
提交回复
热议问题