javabean轉成json字符首字母大寫


今天寫接口的時候有個需求將接口返回的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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM