今天寫接口的時候有個需求將接口返回的json字符串首字母大寫:{"SN":"","Result":""}格式,
只需要在返回bean里面屬性上加上@JsonProperty注解就可以了
import com.fasterxml.jackson.annotation.JsonProperty;
public class DiagResponeBean {
@JsonProperty( "SN")
private String sn;//設備sn
@JsonProperty( "result")
private String result;//響應診斷結果
@JsonProperty( "Region")
private String region;//管理域
@JsonProperty( "Status")
private String status;//設備狀態
//setter/getter
}
//controller 接口部分代碼
com.fasterxml.jackson.databind.ObjectMapper ob =new com.fasterxml.jackson.databind.ObjectMapper();
//json轉bean時忽略大小寫
ob.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
if(StringUtil.isEmpty(json)){
diagResponeBean.setSn("");
diagResponeBean.setResult("入參不能為空");
diagResponeBean.setRegion("");
diagResponeBean.setStatus("");
ob.writeValue(response.getOutputStream(), diagResponeBean);
return;
}
參考博客:https://blog.csdn.net/sinat_35605242/article/details/80826313