Javabean转换成json字符并首字母大写代码实例

论坛 期权论坛 脚本     
nimin   2020-12-20 13:33   194   0

这篇文章主要介绍了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;
      }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持社区。

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1012780
帖子:202556
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP