简体   繁体   English

Spring mvc将json映射到pojo属性为null

[英]Spring mvc mapping json to pojo properties are null

A Rest service is mapped on one url with some @RequestBody where i am mapping json to pojo. Rest服务映射到一个url上,带有一些@RequestBody,我将json映射到pojo。 Pojo contains nested classes following is sample code. Pojo包含嵌套类以下是示例代码。

@RequestMapping(value = "/saveExampleObject.html", method = RequestMethod.POST)
public @ResponseBody List<String> saveExampleObjectDefintion(@RequestBody ExampleObject exampleObject) throws DataAccessException,DataNotPersistException {
    List<String> msg = saveService.save(exampleObject);
    return msg;
}

and the object is like 对象就像

class ExampleObject{
String name;
SubClass subClass;
.....
}

and json is 和json是

{
"name":"name",
"subClass":{
.....
}

I have configured spring mvc annotation and conversion is also happening. 我已经配置了spring mvc注释,转换也在进行中。 But some fields are null . 有些字段为空 I cross checked names of null field they are same as in json and pojo. 我交叉检查空字段的名称,它们与json和pojo中的相同。

PS Only first fields are getting values in subclass.Thanks. PS只有第一个字段在子类中获取值。谢谢。

在你的json中你有subClass但在你的类中你有子类...区分大小写

Here the setters were not defined properly and hence there was an error. 这里的定位器没有正确定义,因此出现了错误。 Spring MVC uses the setters to properly convert POJO to JSON and vice versa. Spring MVC使用setter将POJO正确转换为JSON,反之亦然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM