springboot中关于继承WebMvcConfigurationSupport后自定义的全局Jackson失效解决方法,localdate返回数组问题

一般情况下我们在config里增加jackson的全局配置文件就能满足基本的序列化需求,比如前后端传参的问题。

@Configuration
public class JacksonConfig {

    public static final String LOCAL_TIME_PATTERN = "HH:mm:ss";
    public static final String LOCAL_DATE_PATTERN = "yyyy-MM-dd";
    public static final String LOCAL_DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(ofPattern(LOCAL_TIME_PATTERN)));
        javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(ofPattern(LOCAL_TIME_PATTERN)));
        javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(ofPattern(LOCAL_DATE_PATTERN)));
        javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(ofPattern(LOCAL_DATE_PATTERN)));
        javaTimeModule.addSerializer(LocalDateTime.class,
            new LocalDateTimeSerializer(ofPattern(LOCAL_DATE_TIME_PATTERN)));
        javaTimeModule.addDeserializer(LocalDateTime.class,
            new LocalDateTimeDeserializer(ofPattern(LOCAL_DATE_TIME_PATTERN)));
        objectMapper.registerModule(javaTimeModule);
        objectMapper.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        objectMapper.configure(JsonGenerator.Feature.IGNORE_UNKNOWN, true);
        objectMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configOverride(Boolean.class).setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.NUMBER));

        return objectMapper;
    }

}

但是最近做的项目中发现,请求拦截的时候继承WebMvcConfigurationSupport排除某些例如swagger等目录的情况,上面的配置会失效,所以我们要稍微改动下。看WebMvcConfigurationSupport源码就能发现里面有自己的Jackson处理方法。我们只需要将配置也添加进去就行。

@Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport {

    @Bean
    public HandlerInterceptor getTokenInterceptor() {
        return new TokenInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getTokenInterceptor()).addPathPatterns("/**")
            .addPathPatterns("/**")
            //排查swagger的相关的拦截
            .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");
        super.addInterceptors(registry);
    }


    /**
     * 排查swagger的相关的拦截
     *
     * @param registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
            .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new MappingJackson2HttpMessageConverter(objectMapper()));
    }

    public static final String LOCAL_TIME_PATTERN = "HH:mm:ss";
    public static final String LOCAL_DATE_PATTERN = "yyyy-MM-dd";
    public static final String LOCAL_DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(ofPattern(LOCAL_TIME_PATTERN)));
        javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(ofPattern(LOCAL_TIME_PATTERN)));
        javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(ofPattern(LOCAL_DATE_PATTERN)));
        javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(ofPattern(LOCAL_DATE_PATTERN)));
        javaTimeModule.addSerializer(LocalDateTime.class,
            new LocalDateTimeSerializer(ofPattern(LOCAL_DATE_TIME_PATTERN)));
        javaTimeModule.addDeserializer(LocalDateTime.class,
            new LocalDateTimeDeserializer(ofPattern(LOCAL_DATE_TIME_PATTERN)));
        objectMapper.registerModule(javaTimeModule);
        objectMapper.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        objectMapper.configure(JsonGenerator.Feature.IGNORE_UNKNOWN, true);
        objectMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configOverride(Boolean.class).setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.NUMBER));

        return objectMapper;
    }
}

也就是把以前直接@Bean注入的形式改成了直接在子类Override然后add进去就行。LocalDateTime返回变成数组也是这个原因。

你可能感兴趣的:(自学,spring,boot,后端,java)