无法反序列化HashSet的实例

时间:2020-10-26 00:18:39

标签: java json spring-boot

我创建了这个Json对象:

{
   "firstName":"John",
   "organizations":[
      {
         "name":"ACME inc.",
         "sector":{
            "sectors":{
               "name":"Technologies vertes"
            }
         }
      }
   ]
}

此Json对象应与那些模型类匹配:

@Getter @Setter
public class UserDetailsRequestModel {
    private String firstName;
    private Set<OrganizationRequestModel> organizations;
}

我的OrganizationRequestModel类:

@Getter @Setter
public class OrganizationRequestModel {
    private String name;
    private SectorRequestModel sector;
}

我的SectorRequestModel:

@Getter @Setter
public class SectorRequestModel {
    private Set<SectorLangRequestModel> sectors;
}

最后:

@Getter @Setter
public class SectorLangRequestModel {
    private String name;
}

对于其余的API,我创建了一个UserController用于创建用户:

public UserRestResponseModel createUser(@RequestBody UserDetailsRequestModel userDetails) {
    log.info("createUser() called");
    UserRestResponseModel returnValue;

    ModelMapper modelMapper = new ModelMapper();
    modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
    UserDto userDto = modelMapper.map(userDetails, UserDto.class);

    UserDto createdUser = userService.createUser(userDto);
    returnValue = modelMapper.map(createdUser, UserRestResponseModel.class);
    return returnValue;
}

在添加部门之前,我的用户创建工作正常。即使使用调试器,我也会收到错误消息:

JSON parse error: Cannot deserialize instance of `java.util.HashSet<eu.valoreo.app.ui.model.request.obj.SectorLangRequestModel>` out of START_OBJECT token; nested exception is
com.fasterxml.jackson.databind.exc.MismatchedInputException
at [Source: (PushbackInputStream); line: 31, column: 23] (through reference chain: eu.valoreo.app.ui.model.request.UserDetailsRequestModel["organizations"]->java.util.HashSet[0]->eu.valoreo.app.ui.model.request.obj.OrganizationRequestModel["sector"]->eu.valoreo.app.ui.model.request.obj.SectorRequestModel["sectors"])]

我不太清楚问题出在哪里。我的Json对象看起来正确

1 个答案:

答案 0 :(得分:2)

我的Json似乎不正确:

"sector":{
   "sectors":[{
       "name":"Technologies vertes"
    }]
}