[英]SpringMVC response json not right for Boolean properties
I use SpringMVC 4.2.5, and make a rest Controller, but the response is not what I want. 我使用SpringMVC 4.2.5,并制作一个休息控制器,但响应不是我想要的。 Here it's the detail.
这是细节。 I have an Entity named
propertyEntity
, 我有一个名为
propertyEntity
的实体,
public class PropertyEntity implements Serializable, Cloneable {
private static final long serialVersionUID = -7032855749875735832L;
private int id;
private String propertyName;
private boolean isEnable;
private boolean isDimension;
private boolean isMetric;
}
and the Controller is: 控制器是:
@Controller
@RequestMapping("/api/v1/properties")
public class PropertyController {
@RequestMapping(method = RequestMethod.GET,
produces = "application/json;charset=utf-8")
@ResponseStatus(HttpStatus.OK)
public @ResponseBody
List<PropertyEntity> getAll() {
return propertyService.getAll();
}
}
When I request the api, the result is: 当我请求api时,结果是:
[
{
"id": 1,
"propertyName": "money1",
"isEnable": true,
"dimension": false,
"metric": true
},
{
"id": 2,
"propertyName": "money2",
"isEnable": true,
"dimension": false,
"metric": true
}
]
what I want is: 我想要的是:
[
{
"id": 1,
"propertyName": "money1",
"isEnable": true,
"isDimension": false,
"isMetric": true
},
{
"id": 2,
"propertyName": "money2",
"isEnable": true,
"isDimension": false,
"isMetric": true
}
]
The unexpected thing is: isDimention
is changed to dimension
, isMetric
is changed to metric
, but isEnable
is right. 意想不到的是:
isDimention
更改为dimension
, isMetric
更改为metric
,但isEnable
是正确的。
Change your class to : 将您的班级更改为:
public class PropertyEntity implements Serializable, Cloneable {
...
...
@JsonProperty("isEnable")
private boolean isEnable;
...
...
}
See also : When is the @JsonProperty property used and what is it used for? 另请参见: 何时使用@JsonProperty属性以及它用于什么?
I assume you are using jackson for converting the "PropertyEntity" object to a json. 我假设你使用jackson将“PropertyEntity”对象转换为json。
A possible problem here could be the getters and setters in PropertyEntity class. 这里可能存在的问题可能是PropertyEntity类中的getter和setter。
See the getter/setter of isEnable
and follow a similar naming convention for isMetric
& isDimension
请参阅
isEnable
的getter / setter,并遵循isMetric
和isDimension
的类似命名约定
ensure that the getters of boolean start with isIsMetric()...
instead of getIsMetric()
. 确保boolean的getter以
isIsMetric()...
而不是getIsMetric()
。
If this does not help, please share your getters and setters over here. 如果这没有帮助,请在这里分享您的吸气剂和制定者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.