Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@JsonCreator 'Could not find creator property with name' even with ignoreUnknown = true

Tags:

java

jackson

I have the following class:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Topic {

    private List<Comment> comments = new ArrayList<>();

    private List<User> users = new ArrayList<>();

    @JsonCreator
    public Topic(@JsonProperty("success") boolean success,
                 @JsonProperty("response_comments") List<ResponseComment> responseComments,
                 @JsonProperty("response_users") List<ResponseUser> responseUsers) {

        if (success) {
            comments = Util.resolveComments(responseComments); 
            users = Util.resolveUsers(responseUsers); //some logic
        }

    }

}

When I try to deserialize JSON, it throws:

Could not find creator property with name 'comments' (in class com.test.domain.mapper.Topic)

I don't want to fill comments from json, just in constructor from properties. However, if I write next params:

@JsonProperty("success") boolean success,
@JsonProperty("response_comments") List<ResponseComment> responseComments,
@JsonProperty("response_users") List<ResponseUser> responseUsers,
@JsonProperty("comments") Object a,
@JsonProperty("users") Object a

all works.

like image 447
Feeco Avatar asked Nov 11 '16 10:11

Feeco


3 Answers

Using lomback's @AllArgsConstructor and jackson 2.8.7. Upgrade jackson to 2.8.8 and problem resolved.

like image 119
Daniel Wei Avatar answered Nov 19 '22 01:11

Daniel Wei


After hours of unit testing and copying of classes, I found a solution. I don't want to admit it, but the issue in the Lombok's @AllArgsConstructor. I'm sorry that I didn't provided that I use Lombok at all.
Without @AllArgsConstructor in Topic all works as expected. However, it exists in other classes even with @JsonCreator and works. I'm sorry for your time.

like image 22
Feeco Avatar answered Nov 19 '22 01:11

Feeco


After working on this for a few hours and confirming the behavior experienced by @Feeco, I was able to resolve this issue by upgrading Lombok from v1.16.16 to v1.16.20.

like image 4
Jason Bristol Avatar answered Nov 19 '22 02:11

Jason Bristol