How to de/serialize an immutable object without default constructor using ObjectMapper?

前端 未结 3 1202
悲哀的现实
悲哀的现实 2020-12-04 13:39

I want to serialize and deserialize an immutable object using com.fasterxml.jackson.databind.ObjectMapper.

The immutable class looks like this (just 3 internal attr

3条回答
  •  悲&欢浪女
    2020-12-04 14:31

    To let Jackson know how to create an object for deserialization, use the @JsonCreator and @JsonProperty annotations for your constructors, like this:

    @JsonCreator
    public ImportResultItemImpl(@JsonProperty("name") String name, 
            @JsonProperty("resultType") ImportResultItemType resultType, 
            @JsonProperty("message") String message) {
        super();
        this.resultType = resultType;
        this.message = message;
        this.name = name;
    }
    

提交回复
热议问题