英文:
Cannot deserialize instance of a HashSet
问题
我创建了这个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;
}
@Getter @Setter
public class OrganizationRequestModel {
private String name;
private SectorRequestModel sector;
}
@Getter @Setter
public class SectorRequestModel {
private Set<SectorLangRequestModel> sectors;
}
@Getter @Setter
public class SectorLangRequestModel {
private String name;
}
对于我的REST 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;
}
在我添加了sector字段之后,我的用户创建正常工作。即使使用调试器,我仍然得到一个错误消息:
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对象看起来是正确的。
英文:
I created this Json object:
{
"firstName":"John",
"organizations":[
{
"name":"ACME inc.",
"sector":{
"sectors":{
"name":"Technologies vertes"
}
}
}
]
}
This Json object should match with those model classes :
@Getter @Setter
public class UserDetailsRequestModel {
private String firstName;
private Set<OrganizationRequestModel> organizations;
}
My OrganizationRequestModel class :
@Getter @Setter
public class OrganizationRequestModel {
private String name;
private SectorRequestModel sector;
}
My SectorRequestModel:
@Getter @Setter
public class SectorRequestModel {
private Set<SectorLangRequestModel> sectors;
}
Finally :
@Getter @Setter
public class SectorLangRequestModel {
private String name;
}
For my rest API, i created a UserController for user creation:
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;
}
My user creation was working fine until I added the sector. Even with debugger I obtain an error message:
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"])]
I don't really understand where is the problem. My Json object looks correct
答案1
得分: 2
好的,以下是您要翻译的内容:
似乎我的 JSON 不正确:
"sector": {
"sectors": [{
"name": "Technologies vertes"
}]
}
英文:
It seems that my Json was not correct:
"sector":{
"sectors":[{
"name":"Technologies vertes"
}]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论