java Spring MVC Rest服务JSON将布尔值的默认值设置为true
将boolean
的默认值设置为true
这是dto clas,其中包含布尔值
DTO类
public class SensitivityDto extends AuditableEntity implements Serializable {
private static final long serialVersionUID = 1L;
private long sensitivityId;
@JsonIgnore
private boolean isSelected;
public SensitivityDto() {
}
/** other getter/setters */
public boolean isIsSelected() {
return isSelected;
}
public void setIsSelected(boolean isSelected) {
this.isSelected = isSelected;
}
}
输入json
{
"sensitivity": {
"sensitivityId": 100,
"isSelected": "true", // if not passing this field always its treated as null.
}
}
控制器
public @ResponseBody ResultDecorator saveLabResultCultureDetails(@RequestBody SensitivityDto sensitivityDto) throws Exception {
}
如何将布尔值default设置为true,,这样如果这个值在json中不存在,那么它就不应该是false,而应该是true
# 1 楼答案
那怎么办
来自评论:
您必须删除
@JsonIgnore
并使用@JsonAutoDetect,如下所示: