java – JsonIgnoreProperties不适用于JsonCreator构造函数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – JsonIgnoreProperties不适用于JsonCreator构造函数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2769字,纯文字阅读大概需要4分钟。
内容图文
我有以下实体作为目标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"])
在控制器中我有:
@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"
但杰克逊不知道如何将字符串“9WL4J”映射到对象UserAccountActivationCodeEntity
我猜字符串“9WL4J”是UserAccountActivationCodeEntity的主键id的值,在这种情况下你应该在json中:
"activationCode" : {"id" : "9WL4J"}
如果不是这种情况,请使用自定义Deseralizer告诉Jackson如何将字符串映射到对象.您可以在实体上使用@JsonDeserialize.
内容总结
以上是互联网集市为您收集整理的java – JsonIgnoreProperties不适用于JsonCreator构造函数全部内容,希望文章能够帮你解决java – JsonIgnoreProperties不适用于JsonCreator构造函数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。