Spring mvc Jackson JSON无法反序列化哈希集
我一直在寻找以下异常消息的解决方案:当我向实体发布更新时,我在我的spring boot/spring mvc项目中收到了以下异常消息:Spring mvc Jackson JSON无法反序列化哈希集,spring-mvc,jackson,spring-boot,Spring Mvc,Jackson,Spring Boot,我一直在寻找以下异常消息的解决方案:当我向实体发布更新时,我在我的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.HttpMessa
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
然而,我的案例似乎与我在StackOverflow和Jackson文档中看到的其他案例略有不同
以下是我的两个数据结构:
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;
}
似乎它不喜欢我的employee.user.roles列表中的单个字符串,但添加“@JsonProperty(“roles”)”也没有帮助。文章的正文格式不正确。它包含一个用于角色的JSON对象。JSON对象无法映射到
哈希集
,因此失败哈希集
,因此失败<代码>角色在JSON中需要是一个数组。您可以检查实际的POST请求(例如通过wireshark)吗?我使用Firebug查看GET和POST。有什么特别需要寻找的吗?只是为了检查帖子是否需要JSON。您可以检查实际的帖子请求(例如通过wireshark)吗?我使用Firebug查看GET和POST。有什么特别需要查找的吗?只是为了检查帖子是否需要JSON。我遇到了JSON格式错误的问题。这个答案解决了问题。我也面临同样的问题。。我是否需要将数据结构类型从set更改为list??私有列表角色=新的ArrayList();我遇到了JSON格式错误的问题。这个答案解决了问题。我也面临同样的问题。。我是否需要将数据结构类型从set更改为list??私有列表角色=新的ArrayList();
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
}
[
{
"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":{
...
}
}
]