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;
}
}