zss
2024-08-06 e60938e6bf6edf5da6dd7f9f6cc831fdcf27a69f
framework/src/main/java/com/yuanchu/mom/config/OpenFifer.java
@@ -1,11 +1,25 @@
package com.yuanchu.mom.config;
import org.springframework.beans.factory.annotation.Autowired;
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 {
@@ -16,10 +30,63 @@
    @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<HttpMessageConverter<?>> 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);
    }
}