@JsonCreator'找不到名称为'的创建者属性,即使是ignoreUnknown = true [英] @JsonCreator 'Could not find creator property with name' even with ignoreUnknown = true

查看:720
本文介绍了@JsonCreator'找不到名称为'的创建者属性,即使是ignoreUnknown = true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程:

@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
        }

    }

}

当我尝试反序列化JSON时,它抛出:

When I try to deserialize JSON, it throws:


找不到名称为'comments'的创建者属性(在class com.test.domain.mapper.Topic)

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

一切正常。

推荐答案

经过数小时的单元测试和复制课程,我找到了解决方案。我不想承认,但 Lombok 中的问题 @ AllArgsConstructor 。对不起,我没有提供我完全使用Lombok。
没有 @AllArgsConstructor in 主题一切按预期工作。但是,它存在于其他类中,即使使用 @JsonCreator 也可以。对不起,我很抱歉。

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.

这篇关于@JsonCreator'找不到名称为'的创建者属性,即使是ignoreUnknown = true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆