Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

   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.

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:

[
   {
      "employeeId":1,
      "endDateTime":null,
      "workDays":[

      ],
      "ssn":"111-22-3333",
      "maskedSsn":null,
      "user":{
         "username":"[email protected]",
         "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":"[email protected]",
         "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.

like image 618
sonoerin Avatar asked Dec 01 '25 03:12

sonoerin


1 Answers

The body of the POST is malformed. It contains a JSON object for roles. A JSON object can't be mapped to a HashSet, hence the failure. roles in your JSON needs to be an array.

like image 126
Andy Wilkinson Avatar answered Dec 04 '25 03:12

Andy Wilkinson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!