Cannot deserialize instance of a HashSet

huangapple go评论77阅读模式
英文:

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:

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

This Json object should match with those model classes :

@Getter @Setter
public class UserDetailsRequestModel {
    private String firstName;
    private Set&lt;OrganizationRequestModel&gt; organizations;
}

My OrganizationRequestModel class :

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

My SectorRequestModel:

@Getter @Setter
public class SectorRequestModel {
    private Set&lt;SectorLangRequestModel&gt; 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(&quot;createUser() called&quot;);
    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&lt;eu.valoreo.app.ui.model.request.obj.SectorLangRequestModel&gt;` 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[&quot;organizations&quot;]-&gt;java.util.HashSet[0]-&gt;eu.valoreo.app.ui.model.request.obj.OrganizationRequestModel[&quot;sector&quot;]-&gt;eu.valoreo.app.ui.model.request.obj.SectorRequestModel[&quot;sectors&quot;])]

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:

&quot;sector&quot;:{
   &quot;sectors&quot;:[{
       &quot;name&quot;:&quot;Technologies vertes&quot;
    }]
}

huangapple
  • 本文由 发表于 2020年10月26日 08:18:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64530131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定