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> converters) {
	    converters.add(customJackson2HttpMessageConverter());
	    super.addDefaultHttpMessageConverters(converters);
    }

}

你可能感兴趣的:(JackSon)