无法构造“com.domain.User”的实例(不存在创建者,如默认构造函数):无法从对象值反序列化
Cannot construct instance of `com.domain.User` (no Creators, like default constructor, exist): cannot deserialize from Object value
我有一个控制器接受 ObjectNode
作为 @RequestBody
。
那个ObjectNode
代表json
有一些用户数据
{
"given_name":"ana",
"family_name": "fabry",
"email": "fabry@gmail.com",
"password": "mypass",
"gender": "FEMALE"
}
Controller.java
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public JsonNode createUser(@RequestBody ObjectNode user){
return userService.addUser(user);
}
我想获取用户 ObjectNode
将其转换为 Java POJO
将其保存到数据库并再次 return 将其作为 JsonNode
。
UserServiceImpl.java
private final UserRepository userRepository;
private final UserMapper userMapper;
@Override
public JsonNode addUser(@RequestBody ObjectNode user) {
try {
return userMapper.fromJson(user)
.map(r -> {
final User created = userRepository.save(r);
return created;
})
.map(userMapper::toJson)
.orElseThrow(() -> new ResourceNotFoundException("Unable to find user"));
} catch (RuntimeException re) {
throw re;
}
}
将ObjectNode
转换为POJO
我在 UserMapper
class:
中这样做了
public Optional<User> fromJson(ObjectNode jsonUser) {
User user = objectMapper.treeToValue(jsonUser, User.class);
}
此外,为了将对象写入 JsonNode
,我这样做了:
public JsonNode toJson(User user) {
ObjectNode node = objectMapper.createObjectNode();
node.put("email", user.email);
node.put("password", user.password);
node.put("firstName", user.firstName);
node.put("lastName", user.firstName);
node.put("gender", user.gender.value);
node.put("registrationTime", user.registrationTime.toString());
return node;
}
User.java
@Document(collection = "user")
@Builder
@AllArgsConstructor
public class User {
@Indexed(unique = true)
public final String email;
@JsonProperty("password")
public final String password;
@JsonProperty("firstName")
public final String firstName;
@JsonProperty("lastName")
public final String lastName;
@JsonProperty("gender")
public final Gender gender;
@JsonProperty("registrationTime")
public final Instant registrationTime;
public static User createUser(
String email,
String password,
String firstName,
String lastName,
Gender gender,
Instant registrationTime){
return new User(email, password, firstName, lastName, gender, registrationTime);
}
}
当我 运行 我的应用程序时,这是我收到的错误:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.domain.User` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
我已经阅读了有关该错误的信息,似乎出现此错误是因为 Jackson 库不知道如何创建一个没有空构造函数的模型,并且该模型包含一个带有参数的构造函数,我对其进行了注释参数 @JsonProperty("fieldName")
。但即使在应用 @JsonProperty("fieldName")
之后,我仍然遇到同样的错误。
我已经将 ObjecatMapper 定义为 Bean
@Bean
ObjectMapper getObjectMapper(){
return new ObjectMapper();
}
我在这里错过了什么?
注册 Jackson ParameterNamesModule,它会自动将 JSON 属性映射到相应的构造函数属性,因此将允许您使用不可变的 类.
我可以重现异常。然后我添加了一个全参数构造函数,每个参数都用正确的 @JsonProperty
.
注释
@JsonCreator
public User(
@JsonProperty("email") String email,
@JsonProperty("password") String password,
@JsonProperty("firstName") String firstName,
@JsonProperty("lastName") String lastName,
@JsonProperty("gender") String gender,
@JsonProperty("registrationTime") Instant registrationTime){
super();
this.email = email;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.gender = gender;
this.registrationTime = registrationTime;
}
现在,它创建了实例,但我收到其他映射错误(无法识别的字段“given_name”),您应该能够解决这些错误。
我有一个控制器接受 ObjectNode
作为 @RequestBody
。
那个ObjectNode
代表json
有一些用户数据
{
"given_name":"ana",
"family_name": "fabry",
"email": "fabry@gmail.com",
"password": "mypass",
"gender": "FEMALE"
}
Controller.java
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public JsonNode createUser(@RequestBody ObjectNode user){
return userService.addUser(user);
}
我想获取用户 ObjectNode
将其转换为 Java POJO
将其保存到数据库并再次 return 将其作为 JsonNode
。
UserServiceImpl.java
private final UserRepository userRepository;
private final UserMapper userMapper;
@Override
public JsonNode addUser(@RequestBody ObjectNode user) {
try {
return userMapper.fromJson(user)
.map(r -> {
final User created = userRepository.save(r);
return created;
})
.map(userMapper::toJson)
.orElseThrow(() -> new ResourceNotFoundException("Unable to find user"));
} catch (RuntimeException re) {
throw re;
}
}
将ObjectNode
转换为POJO
我在 UserMapper
class:
public Optional<User> fromJson(ObjectNode jsonUser) {
User user = objectMapper.treeToValue(jsonUser, User.class);
}
此外,为了将对象写入 JsonNode
,我这样做了:
public JsonNode toJson(User user) {
ObjectNode node = objectMapper.createObjectNode();
node.put("email", user.email);
node.put("password", user.password);
node.put("firstName", user.firstName);
node.put("lastName", user.firstName);
node.put("gender", user.gender.value);
node.put("registrationTime", user.registrationTime.toString());
return node;
}
User.java
@Document(collection = "user")
@Builder
@AllArgsConstructor
public class User {
@Indexed(unique = true)
public final String email;
@JsonProperty("password")
public final String password;
@JsonProperty("firstName")
public final String firstName;
@JsonProperty("lastName")
public final String lastName;
@JsonProperty("gender")
public final Gender gender;
@JsonProperty("registrationTime")
public final Instant registrationTime;
public static User createUser(
String email,
String password,
String firstName,
String lastName,
Gender gender,
Instant registrationTime){
return new User(email, password, firstName, lastName, gender, registrationTime);
}
}
当我 运行 我的应用程序时,这是我收到的错误:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.domain.User` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
我已经阅读了有关该错误的信息,似乎出现此错误是因为 Jackson 库不知道如何创建一个没有空构造函数的模型,并且该模型包含一个带有参数的构造函数,我对其进行了注释参数 @JsonProperty("fieldName")
。但即使在应用 @JsonProperty("fieldName")
之后,我仍然遇到同样的错误。
我已经将 ObjecatMapper 定义为 Bean
@Bean
ObjectMapper getObjectMapper(){
return new ObjectMapper();
}
我在这里错过了什么?
注册 Jackson ParameterNamesModule,它会自动将 JSON 属性映射到相应的构造函数属性,因此将允许您使用不可变的 类.
我可以重现异常。然后我添加了一个全参数构造函数,每个参数都用正确的 @JsonProperty
.
@JsonCreator
public User(
@JsonProperty("email") String email,
@JsonProperty("password") String password,
@JsonProperty("firstName") String firstName,
@JsonProperty("lastName") String lastName,
@JsonProperty("gender") String gender,
@JsonProperty("registrationTime") Instant registrationTime){
super();
this.email = email;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.gender = gender;
this.registrationTime = registrationTime;
}
现在,它创建了实例,但我收到其他映射错误(无法识别的字段“given_name”),您应该能够解决这些错误。