Spring通过ResponseBody返回对象时,JSON属性变为首字母大写

1 背景

1.1 版本信息

  • springcloud版本:Dalston.SR1
  • springboot版本:1.5.3.Release
  • JDK版本:1.8

1.2 缺陷描述

通过success等方法返回的json数据的首字母均为大写,十分离奇。但该dto对象并无特别之处,具体代码如下所示

@Setter
@Getter
@ApiModel("返回结果")
public class ResultObject<T> implements Serializable {

    private static final long serialVersionUID = 3842145580241595212L;
    /**
     * 状态码
     */
    @ApiModelProperty("状态码")
    private Integer code;
    /**
     * 消息
     */
    @ApiModelProperty("消息")
    private String msg;
    /**
     * 数据
     */
    @ApiModelProperty("数据")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonProperty(value = "data")
    private T data;

    /**
     * 请求成功
     */
    public static <T> ResultObject<T> success(T data) {
        ResultObject<T> resultObject = new ResultObject<T>();
        resultObject.setCode(0);
        resultObject.setMsg("请求成功");
        resultObject.setData(data);
        return resultObject;
    }

    /**
     * 请求失败
     */
    public static <T> ResultObject<T> error(T data) {
        ResultObject<T> resultObject = new ResultObject<T>();
        resultObject.setCode(1);
        resultObject.setMsg("请求失败");
        resultObject.setData(data);
        return resultObject;
    }

    public static ResultObject exception(Integer code, String message) throws JsonProcessingException {
        ResultObject resultObject = new ResultObject();
        resultObject.setCode(code);
        resultObject.setMsg(message);
        return resultObject;
    }
}

请求结果如下图
在这里插入图片描述

2 原因排查

经排查,只要是直接通过spring的responseBody返回的对象都是属性大写开头,遂想到json的配置是否存在问题。黄天不负苦心人,找到如下配置类。正是他导致如此奇怪的结果:

@Configuration
public class JsonTransferConfigurer {

    @Bean
    public ObjectMapper simpleObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("yyyyMMddHHmmss"));
        objectMapper.setSerializationInclusion(Include.NON_NULL);
        objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        return objectMapper;
    }

    @Bean
    @Primary
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("yyyyMMddHHmmss"));
        objectMapper.setSerializationInclusion(Include.NON_NULL);
        objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        return objectMapper;
    }
}

3 总结

只需修改PropertyNamingStrategy的策略即可,修改为LOWER_CAMEL_CASE

@Configuration
public class JsonTransferConfigurer {

    @Bean
    public ObjectMapper simpleObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("yyyyMMddHHmmss"));
        objectMapper.setSerializationInclusion(Include.NON_NULL);
        objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        return objectMapper;
    }

    @Bean
    @Primary
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("yyyyMMddHHmmss"));
        objectMapper.setSerializationInclusion(Include.NON_NULL);
        objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        return objectMapper;
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值