[英]Jackson JSON unable to deserialize hashset
I have been looking for a solution to the following exception message I am getting on my spring-boot/spring-mvc project when I POST an update to an entity: 我一直在寻找以下异常消息的解决方案,当我向实体发布更新时,我在spring-boot / spring-mvc项目中得到了该消息:
o.s.w.s.m.m.a.HttpEntityMethodProcessor : Written [{timestamp=Wed Jan 14 11:15:34 MST 2015, status=400, error=Bad Request, exception=org.springframework.http.converter.HttpMessageNotReadableException, message=Could not read JSON: Can not deserialize instance of java.util.HashSet out of START_OBJECT token
at [Source: org.apache.catalina.connector.CoyoteInputStream@7f5840c4; line: 1, column: 146] (through reference chain: com.company.product.model.people.dto.EmployeeDTO["user"]->com.company.product.security.dto.UserDTO["roles"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.HashSet out of START_OBJECT token
However, it would seem my case is slightly different than the others I have seen on StackOverflow and the Jackson documentation. 但是,看来我的情况与我在StackOverflow和Jackson文档中看到的其他情况略有不同。
Here are my two data structures: 这是我的两个数据结构:
public class EmployeeDTO {
List<WorkdayDTO> workDays = new ArrayList<WorkdayDTO>();
private UserDTO user;
...
public List<WorkdayDTO> getWorkDays() {
return workDays;
}
public void setWorkDays(List<WorkdayDTO> workDays) {
this.workDays = workDays;
}
public UserDTO getUser() {
return user;
}
public void setUser(UserDTO user) {
this.user = user;
}
} }
And: 和:
public class UserDTO {
private boolean credentialsNonExpired;
private OfficeDTO office;
@JsonProperty("roles") // <-- added this based on early research, hasn't helped though
private Set<String> roles = new HashSet<String>();
// .. standard getters & setters
}
When I do a GET my json response is this: 当我执行GET时,我的json响应是这样的:
[
{
"employeeId":1,
"endDateTime":null,
"workDays":[
],
"ssn":"111-22-3333",
"maskedSsn":null,
"user":{
"username":"user@test.com",
"password":null,
"enabled":true,
"accountNonExpired":true,
"accountNonLocked":true,
"credentialsNonExpired":true,
"office":null,
"roles":[
"OWNER"
]
},
"address":{
...
}
},
{
"employeeId":2,
"endDateTime":null,
"workDays":[
...
],
"ssn":"333-44-5555",
"maskedSsn":null,
"user":{
"username":"userA@test.com",
"password":null,
"enabled":true,
"accountNonExpired":true,
"accountNonLocked":true,
"credentialsNonExpired":true,
"office":null,
"roles":[
"MANAGER"
]
},
"address":{
...
}
}
]
It seems like it doesn't like the single string in my employee.user.roles list, but adding that " @JsonProperty("roles")" didn't help either. 似乎不喜欢我的employee.user.roles列表中的单个字符串,但是添加“ @JsonProperty(“ roles”)“也不起作用。
The body of the POST is malformed. POST的正文格式不正确。 It contains a JSON object for
roles
. 它包含用于
roles
的JSON对象。 A JSON object can't be mapped to a HashSet
, hence the failure. JSON对象无法映射到
HashSet
,因此失败。 roles
in your JSON needs to be an array. JSON中的
roles
必须是一个数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.