美文网首页
springboot处理json的时间格式

springboot处理json的时间格式

作者: 指尖架构141319 | 来源:发表于2020-03-11 17:40 被阅读0次

1.引用的json包

- springboot默认使用的是jackson
- 改为fastjson

2.针对性使用

2.1 jackson两种配置
  • apllication.property中配置
   spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
  spring.jackson.time-zone=GMT+8
  • 实体类中
@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
@DateTimeFormat(pattern="yyyy-MM-dd")//接受格式
  private Date createTime;
2.2 fastjson中配置
  • 全局配置
    在json的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;

相关文章

网友评论

      本文标题:springboot处理json的时间格式

      本文链接:https://www.haomeiwen.com/subject/fcrhjhtx.html