- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有以下实体,我将其用作对 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/
我有一个 POJO,例如 public class Category { public Collection items; public static class Item {
我正在使用 Jackson将 JSON 字符串反序列化为枚举。 public enum RoomType { SHARED("shared"), PRIVATE("private");
初始问题 是否可以有多个 @JsonCreator 方法,并让 jackson 根据方法定义检测应该使用哪一个? @JsonCreator public static StateOfComm fact
我正在尝试使用 Jackson 将一些 JSON 转换为一个类的实例,该类包含一些简单的字符串和另一个我正在使用 @JsonCreator 的类。看来 Jackson 无法创建其他类的实例。 问题是当
我正在尝试使用 jackson 将 json 反序列化为枚举。如果工厂方法只有一个参数,它工作正常。一旦我们添加更多参数,它就会停止工作。 这是我试过的代码示例。 public enum Test {
我正在尝试使用 Jackson 1.9.10 反序列化此类的一个实例: public class Person { @JsonCreator public Person(@JsonProperty("
我在使 RestEasy (3.0.10.Final) 将路径参数解析为枚举值时遇到问题。 有了枚举定义... package com.stines; import com.fasterxml.jac
我想使用主要构造函数用 @JsonCreator 进行注释,如下所示: // error @JsonCreator class User( @JsonProperty("username")
我在构造函数的参数上使用Jackson @JsonProperty(name) 注释,我遇到的每个文档都指出我们必须使用@JsonCreator 注释和@JsonProperty 才能正常工作,但即使
我很想用这个: @Getter @ToString @RequiredArgsConstructor(onConstructor_ = {@JsonCreator(mode =
我正在编写一个简单的 REST API,它将在给定主机名上安装/启动/停止代理。作为 API 的一部分,我正在创建一个 AgentInstruction 实例(代码如下)。由于某种原因,应用程序抛出
当我在嵌套字段上使用@JsonUnwrapped时: public class Person{ private int id; @JsonUnwrapped private F
我正在使用带有 JavaConfig 和 Jackson 2.4.3 的 Spring 4.1.1。 我的 Controller 看起来像这样: @RestController public inte
Jackson 似乎使用反射将附加属性直接写入字段,即使 @JsonCreator使用了构造函数,该字段标记为 final . 例子: public class Test { static c
我有以下实体,我将其用作对 Controller 的请求之一的目标 POJO: Entity @Table(name="user_account_entity") @JsonIgnorePropert
我正在使用 Jackson 序列化/反序列化不可变的 POJO。因为 POJO 是不可变的,所以我这样定义它们: class Foo { private final String bar;
我目前正在使用 Jackson 1.4.2 并尝试反序列化从我们的 UI 传回 Java Controller (Servlet) 的 code 值(类型信息的唯一标识符)。 有多种类型(例如 ABC
我有一个 Value 类,它包含一个值: public class Value { protected final Object value; @JsonValue publi
我有一些运行良好的旧代码,但现在我想在域对象中使用@JsonCreator,并且我能够重构所有内容,但我现在陷入了一个问题。在一种情况下,我的 Json 结构如下: [{ "name" :
我有一个包含很多字段的可序列化对象,其中一些是强制性的,其他是可选的。为了使这个对象易于使用,我尝试了 Builder Pattern with a Twist 。这很有效,但是生成的某些 Build
我是一名优秀的程序员,十分优秀!