java - JsonIgnoreProperties 不适用于 JsonCreator 构造函数

标签 java spring

我有以下实体,我将其用作对 Controller 的请求之一的目标 POJO:

Entity
@Table(name="user_account_entity")
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonSerialize(using = UserAccountSerializer.class)
public class UserAccountEntity implements UserDetails {
    //...
    private String username;

    private String password;

    @PrimaryKeyJoinColumn
    @OneToOne(mappedBy= "userAccount", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private UserEntity user;

    @PrimaryKeyJoinColumn
    @OneToOne(mappedBy= "userAccount", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private UserAccountActivationCodeEntity activationCode;

    @JsonCreator
    public UserAccountEntity(@JsonProperty(value="username", required=true) final String username, @JsonProperty(value="password", required=true) final String password) {
      //....
    }

    public UserAccountEntity() {}

    //.....
}

当我在请求中放入意外字段时,它会抛出 MismatchedInputException 并失败并显示以下消息:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.myproject.project.core.entity.userAccountActivationCode.UserAccountActivationCodeEntity` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('9WL4J')
 at [Source: (PushbackInputStream); line: 4, column: 20] (through reference chain: com.myproject.project.core.entity.userAccount.UserAccountEntity["activationCode"])

在我的 Controller 中:

@InitBinder
public void binder(WebDataBinder binder) {
    binder.addValidators(new CompoundValidator(new Validator[] {
            new UserAccountValidator(),
            new UserAccountActivationCodeDTOValidator() }));
}

我向其发出请求的端点是:

@Override
public UserAccountEntity login(@Valid @RequestBody UserAccountEntity account,
        HttpServletResponse response) throws MyBadCredentialsException, InactiveAccountException {
    return userAccountService.authenticateUserAndSetResponsenHeader(
            account.getUsername(), account.getPassword(), response);
}

更新 1

UserAccountSerializer 的代码:

public class UserAccountSerializer extends StdSerializer<UserAccountEntity> {

    public UserAccountSerializer() {
        this(null);
    }

    protected UserAccountSerializer(Class<UserAccountEntity> t) {
        super(t);
    }

    @Override
    public void serialize(UserAccountEntity value, JsonGenerator gen,
            SerializerProvider provider) throws IOException {
        gen.writeStartObject();
        gen.writeStringField("id", value.getId());
        gen.writeStringField("username", value.getUsername());
        gen.writeEndObject();
    }

}

最佳答案

错误被触发是因为你的 json 中有:

"activationCode" : "9WL4J"

但是 Jackson 不知道如何将字符串“9WL4J”映射到对象 UserAccountActivationCodeEntity

我猜字符串“9WL4J”是 UserAccountActivationCodeEntity 的主键 ID 的值,在这种情况下,您应该在 json 中包含:

"activationCode" : {"id" : "9WL4J"}

如果不是这种情况,请使用自定义反序列化器告诉 Jackson 如何将字符串映射到对象。您可以在实体上使用 @JsonDeserialize。

关于java - JsonIgnoreProperties 不适用于 JsonCreator 构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52661057/

相关文章:

java - 配置 Jetty 以访问共享对象池

java - 分层/链接图

javascript - ES6 获取 : How do I change the localhost port it calls?

java - 从不同于类路径的位置加载 spring XML 以在 Servlet 中获取 bean

java - Maven 项目 - Spring MVC 错误

java - Maven:添加用于编译测试的依赖项,但不用于运行测试

java - 伊巴蒂斯 : Is there a way of adding a rowhandler in an Ibatis resultmap subselect within the sqlmap xml?

java - 将 ListView 和 ArrayAdapter 传递给另一个类

java - maven多模块中的js文件无故变得错误

java - 如何使用 Jquery 向 spring mvc 应用程序发送 POST 请求