1

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.

ishanbakshi
  • 1,915
  • 3
  • 19
  • 37
Skyler Tao
  • 33
  • 1
  • 8

2 Answers2

0

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?

Community
  • 1
  • 1
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
  • I tried adding @JsonProperty("isMetric"), yes, it turns out to `isMetric` result, but `metric` is still there, so I think the reason is what @ishanbakshi says. Thanks all the same, and for the useful link. – Skyler Tao Aug 24 '16 at 07:28
0

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.

Community
  • 1
  • 1
ishanbakshi
  • 1,915
  • 3
  • 19
  • 37
  • Yes, it's the getters and setters method name, change to getIsMetric() and setIsMetric(boolean isMetric) solves the problem. Thanks! – Skyler Tao Aug 24 '16 at 07:25