ObjectMapper can't deserialize without default constructor after upgrade to Spring Boot 2

前端 未结 7 1586
予麋鹿
予麋鹿 2020-12-24 01:42

I have following DTOs:

@Value
public class PracticeResults {
    @NotNull
    Map wordAnswers;
}

@Value
public class ProfileMetaDto {

         


        
7条回答
  •  醉梦人生
    2020-12-24 01:44

    In case you have some final fields within your class and you would rather have Object creator to use specific constructor to deserialize your POJO along with @Data annotation, I suggest annotating the constructor with @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) and concrete required constructor parameters with @JsonProperty(""). The example can be seen below:

    package test.model;
    
    import com.fasterxml.jackson.annotation.JsonCreator;
    import com.fasterxml.jackson.annotation.JsonProperty;
    import lombok.Data;
    
    import java.util.Date;
    
    @Data
    public class KafkaEventPojo {
        private final String executionId;
        private final String folder;
        private final Date created_at;
    
        private Date updated_at;
        // ... other params
        
        /**
         * Instantiates a new Kafka event pojo.
         *
         * @param executionId the execution or flow id of the event lifecycle
         * @param folder      the folder to be retrieved
         */
        @JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
        public KafkaEventPojo(@JsonProperty("executionId") String executionId, @JsonProperty("folder") String folder) {
            this(executionId, folder, new Date(System.currentTimeMillis()));
        }
    
        private KafkaEventPojo(String executionId, String folder, Date date) {
            this.executionId = executionId;
            this.folder = folder;
    
            this.created_at = date;
            this.updated_at = date;
        }
        
        // ... other logic
    }
    

提交回复
热议问题