jackson时间格式化

jackson格式化时间时区问题

一般配置方式

一般在SpringBoot项目中,spring默认使用jackson转换日期,默认为格林威治时间,非东八区时间;通过对jackson的如下配置可在格式化时间时调整为东八区的时间。

1、在对应时间字段上加jackson的格式化注解

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date startTime;

2、在项目配置文件中进行jackson的配置,application.yml

spring:
  jackson:
	time-zone: GMT+8

自定义拦截器后的jackson配置

但若在SpringBoot项目中有自定义继承于WebMvcConfigurationSupport的拦截器之后,原配置文件中jackson配置会失效,即上面2中的配置失效。所以需要在自定义实现类中再次对jackson进行配置,配置如下:

自定义的继承于WebMvcConfigurationSupport的拦截器:

@Configuration
public class ResourcesConfig extends WebMvcConfigurationSupport {

    @Autowired
    private RepeatSubmitInterceptor repeatSubmitInterceptor;
    
	//自定义拦截器实现防止重复提交
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
	    registry.addInterceptor(repeatSubmitInterceptor).addPathPatterns("/**");
	    super.addInterceptors(registry);
    }
    
	//静态资源映射
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
	    registry.addResourceHandler("resource/**").addResourceLocations("file:" + "/Users/resource/uploadPath/");
	    registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
	    registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
	    registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
	    super.addResourceHandlers(registry);
    }
}

添加jackson配置后:

@Configuration
public class ResourcesConfig extends WebMvcConfigurationSupport {

    @Autowired
    private RepeatSubmitInterceptor repeatSubmitInterceptor;
    
	//自定义拦截器实现防止重复提交
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
	    registry.addInterceptor(repeatSubmitInterceptor).addPathPatterns("/**");
	    super.addInterceptors(registry);
    }
    
    //静态资源映射
	@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
	    registry.addResourceHandler("resource/**").addResourceLocations("file:" + "/Users/resource/uploadPath/");
	    registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
	    registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
	    registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    	super.addResourceHandlers(registry);
    }
    
    /**
     * JackSon统一处理时间时区问题
     * @return
     */
    @Bean
    public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
	    MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
	    ObjectMapper objectMapper = new ObjectMapper();
		//对序列化不存在的字段 是否抛出异常
	    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
	    objectMapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
		//属性名序列化策略:LOWER_CAMEL_CASE 除首个单词外,首字母大写
	    objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE);
	    objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
	    objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
	    jsonConverter.setObjectMapper(objectMapper);
	    return jsonConverter;
    }
    
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
	    converters.add(customJackson2HttpMessageConverter());
	    super.addDefaultHttpMessageConverters(converters);
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值