2016-08-24 94 views
1

我使用SpringMVC 4.2.5,并使一个休息控制器,但响应不是我想要的。 这是细节。 我有一个实体命名propertyEntitySpringMVC的响应JSON不正确的布尔属性

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; 
} 

和控制器是:

@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(); 
    } 
} 

当我请求API,其结果是:

[ 
    { 
     "id": 1, 
     "propertyName": "money1", 
     "isEnable": true, 
     "dimension": false, 
     "metric": true 
    }, 
    { 
     "id": 2, 
     "propertyName": "money2", 
     "isEnable": true, 
     "dimension": false, 
     "metric": true 
    } 
] 

我想要的是:

[ 
    { 
     "id": 1, 
     "propertyName": "money1", 
     "isEnable": true, 
     "isDimension": false, 
     "isMetric": true 
    }, 
    { 
     "id": 2, 
     "propertyName": "money2", 
     "isEnable": true, 
     "isDimension": false, 
     "isMetric": true 
    } 
] 

意想不到的是: isDimention更改为dimension, isMetric更改为metric, 但isEnable是正确的。

+1

尝试在场上使用@JsonProperty(“isEnable”) – Naruto

+0

奇怪。如果只有其中一个属性表现如此,那么这看起来像是一个错误。 – dbreaux

回答

0

我假设你正在使用杰克逊的 “PropertyEntity” 对象转换为JSON。

可能的问题可能是PropertyEntity类中的getter和setter。

isEnable的的getter/setter和遵守相似的命名约定isMetric & isDimension

确保布尔开始与isIsMetric()...代替getIsMetric()的干将。

如果这没有帮助,请在此处分享您的获得者和安装者。

+0

是的,这是getters和setters方法的名称,更改为getIsMetric()和setIsMetric(boolean isMetric)解决了问题。谢谢! –

0

更改类:

public class PropertyEntity implements Serializable, Cloneable { 
    ... 
    ... 
    @JsonProperty("isEnable") 
    private boolean isEnable; 
    ... 
    ... 
} 

参见:When is the @JsonProperty property used and what is it used for?

+0

我试着添加@JsonProperty(“isMetric”),是的,它结果是'isMetric'结果,但是'metric'仍然存在,所以我认为原因是@ishanbakshi说的。感谢所有相同的,并为有用的链接。 –