使用WebMvcConfigurer的方法将converter注入到项目中
@Configuration
public class ConverterConfig implements WebMvcConfigurer {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/pages/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/js/**", "/images/**", "/css/**").addResourceLocations("/js/", "/images/", "/css/").resourceChain(true).addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"));
}
/**
* jackson的json转化date的converter接口
*
* @return
*/
// @Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder = new Jackson2ObjectMapperBuilder();
ObjectMapper objectMapper = jackson2ObjectMapperBuilder.build();
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
DateFormat dateFormat = objectMapper.getDateFormat();
objectMapper.setDateFormat(new MyDateFormat(dateFormat));
mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
List<MediaType> list = new ArrayList<>();
list.add(MediaType.APPLICATION_JSON);
mappingJackson2HttpMessageConverter.setSupportedMediaTypes(list);
return mappingJackson2HttpMessageConverter;
}
/**
* 增加到我们配置内部
*
* @param converters
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(mappingJackson2HttpMessageConverter());
}
}