Skyler Tao Skyler Tao - 3 months ago 8
JSON Question

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.
Here it's the detail.
I have an Entity named

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:

[
{
"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.

Answer

I assume you are using jackson for converting the "PropertyEntity" object to a json.

A possible problem here could be the getters and setters in PropertyEntity class.

See the getter/setter of isEnable and follow a similar naming convention for isMetric & isDimension

ensure that the getters of boolean start with isIsMetric()... instead of getIsMetric().

If this does not help, please share your getters and setters over here.