简体   繁体   中英

Problem Jackson deserialization of JSON and Lombok constructor

Having a problem with the Jackson deserialization of my Dto. I'm using Lombok 1.18.16 and Jackson (jackson-databind) 2.11.3

My classes look like this:


    @Data
    public class Dto {
    
        @JsonProperty("ID")
        private Long id;
    
        @JsonProperty("STATUS")
        private Long status 
    
        @JsonProperty("CONTACT_ID")
        private Long contactId;
    
        @JsonProperty("TITLES")
        private List<Text> titles;
    
        @JsonProperty("DESCRIPTIONS")
        private List<Text> descriptions;
    }

The text-Class is this:


    @Data(staticConstructor = "of")
    public class Text {
    
        @JsonProperty("ID")
        private final Long id;
    
        @JsonProperty("LABEL")
        private final String label;
    }

When Jackson is parsing this, I'm getting the exception stating the following:

Cannot construct instance of `com.mycompany.Text` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
 at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 35, column: 13] (through reference chain: com.mycompany.Dto["TITLES"]->java.util.ArrayList[0])

I understand it has to do with the constructor of my Text-class, but I am not sure why - because I am providing the constructor through Lombok.

The JSON I use looks like this:


    {
        "ID": "1",
        "STATUS": "1",
        "CONTACT_ID": "1",
        "TITLES": [
            {
                "ID": 215,
                "LABEL": "Title"
            }
        ],
        "DESCRIPTIONS": [
            { "ID":"0", "LABEL":"Description" }
        ]
    
    }

What am I missing? Also, is there a way to create an optional param for the constructor using Lombok? This would be the case for when I would like to create a new title (then the id is not existing yet).

Thanks for any input!

You are missing @NoArgsConstructor.

@NoArgsConstructor
public class Text {
   ...
}

When you use @data , you are including @Getter , @Setter , @ToString , @EqualsAndHashCode and @RequiredArgsConstructor since jackson needs in order to deserializate a default constructor you should add @NoArgsConstructor.

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