【问题标题】:Jackson JSON unable to deserialize hashsetJackson JSON 无法反序列化哈希集
【发布时间】:2015-03-13 01:26:29
【问题描述】:

当我向实体发布更新时,我在 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

但是,我的情况似乎与我在 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;
    }

}

还有:

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
}

当我执行 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":{
        ...
      }
   }
]

似乎它不喜欢我的employee.user.roles 列表中的单个字符串,但是添加“@JsonProperty("roles")”也没有帮助。

【问题讨论】:

  • 你能检查实际的 POST 请求(例如通过 wireshark)吗?
  • 我使用 Firebug 查看 GET 和 POST。有什么特别要找的吗?
  • 只是为了检查 POST 是否为 JSON。

标签: spring-mvc jackson spring-boot


【解决方案1】:

POST 的正文格式不正确。它包含roles 的 JSON 对象。 JSON 对象无法映射到HashSet,因此失败。您的 JSON 中的 roles 需要是一个数组。

【讨论】:

  • 我遇到了同样的问题,即 JSON 格式错误。这个答案解决了。
  • 我面临同样的问题。我需要将数据结构类型从集合更改为列表吗?私有 List 角色 = new ArrayList();
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-14
  • 2018-12-16
  • 2013-01-08
  • 1970-01-01
  • 1970-01-01
  • 2018-11-14
  • 1970-01-01
相关资源
最近更新 更多