package com.yuanchu.mom.config; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import javax.annotation.Resource; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.List; @Configuration public class OpenFifer extends WebMvcConfigurationSupport { @Resource private FiferConfig fiferConfig; @Resource private PowerConfig powerConfig; @Resource private LogConfig logConfig; @Value("${file.path}") private String filePath; @Value("${outPath}") private String outPath; @Value("${wordUrl}") private String wordUrl; @Value("${spring.jackson.date-format}") private String pattern; public void addResourceHandlers(ResourceHandlerRegistry registry) { //配置拦截器访问静态资源 registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); //设置文件虚拟路径映射 registry.addResourceHandler("/img/**").addResourceLocations("file:"+filePath+"/"); registry.addResourceHandler("/outPath/**").addResourceLocations("file:"+outPath); registry.addResourceHandler("/word/**").addResourceLocations("file:"+wordUrl+"/"); } @Override protected void addInterceptors(InterceptorRegistry registry) { // TODO: 2024/6/4 访问swagger需关下面拦截器 // registry.addInterceptor(fiferConfig).addPathPatterns("/**"); // registry.addInterceptor(powerConfig).addPathPatterns("/**"); registry.addInterceptor(logConfig).addPathPatterns("/**"); super.addInterceptors(registry); } // 全局格式化处理 @Override protected void extendMessageConverters(List> converters) { Jackson2ObjectMapperBuilder json = Jackson2ObjectMapperBuilder.json(); MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); //localDateTime格式化 JavaTimeModule module = new JavaTimeModule(); LocalDateTimeDeserializer dateTimeDeserializer = new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(pattern)); LocalDateTimeSerializer dateTimeSerializer = new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern)); module.addDeserializer(LocalDateTime.class, dateTimeDeserializer); module.addSerializer(LocalDateTime.class, dateTimeSerializer); ObjectMapper objectMapper = json.modules(module) .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).build(); //date时间格式化 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.setDateFormat(new SimpleDateFormat(pattern)); // 设置格式化内容 converter.setObjectMapper(objectMapper); converters.add(0, converter); } }