引用处:
https://blog.csdn.net/molashaonian/article/details/53025118
https://blog.csdn.net/henianyou/article/details/81945409
- 解析JSON的方式:
这里一共有两种不同解析方式 Jackson 和 FastJson两种方式
- Jackson的方式:SpringBoot 默认的json处理是 jackson 也就是对configureMessageConverters 没做配置;
全局配置:
可以在apllication.property加入下面配置就可以了
#时间戳统一转换
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
#时区指定
spring.jackson.time-zone=GMT+8
局部配置:
可以在指定属性的get方法上加注解的方式
(注意:如果在属性上加,那么会出现响应结果包含该属性大写,小写两个字段,如:Result:{Ao:xxx,ao:xxx}
)
@JsonFormat(timezone = “GMT+8”, pattern = “yyyyMMddHHmmss”)
@JsonProperty("Date") //用来指定该属性的json序列化字段名,比如:不进行首字母小写转换
public Date getDate() {
return Date;
}
- FastJson的方式:需要更改configureMessageConverters 配置为FasJson
全局配置:
//mvc config 配置类
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
//修改默认转换的配置
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullStringAsEmpty
);
//此处是全局处理方式
fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
fastConverter.setFastJsonConfig(fastJsonConfig);
List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
supportedMediaTypes.add(MediaType.ALL); // 全部格式
fastConverter.setSupportedMediaTypes(supportedMediaTypes);
converters.add(fastConverter);
}
}
局部配置:
@JSONField(format=”yyyyMMdd”)
private Date createTime;
//如果也出现json响应字段多的问题,那么也加在get方法上