简体   繁体   English

杰克逊JSON无法反序列化哈希集

[英]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.

相关问题 无法使用杰克逊反序列化包含2个具有相同ID的对象的Json - Unable to deserialize Json that contain 2 objects with the same ID using jackson json使用jackson api反序列化 - json deserialize using jackson api 如何与Hibernate一起正确反序列化Jackson(JSON) - How to properly deserialize Jackson (JSON) together with Hibernate 使用org.codehaus.jackson反序列化JSON字符串 - Deserialize JSON String using org.codehaus.jackson Spring无法在Maven中使用杰克逊转换JSON - Spring unable to convert JSON with Jackson present in maven Jackson 反序列化 LocalDateTime - Jackson deserialize LocalDateTime 在修改消息转换器后,Jackson + Spring Web无法反序列化未加引号的字符串 - Jackson + Spring web unable to deserialize unquoted string after modifying message-converters 无法使用Spring MVC / jackson序列化JSON以正确设置 - unable to serialize JSON to Set correctly with Spring MVC/jackson 我有一个字符串:“ 2012年2月14日”,我想通过spring / json / jackson反序列化到joda-time LocalDate - I have a string : “14 Feb 2012” that I want to deserialize to a joda-time LocalDate via spring/json/jackson 强制杰克逊将特定字段反序列化,即使它为null? - force jackson to deserialize a particular field even it is null?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM