chenrui
2025-03-05 7374d150b7f08d4b126d3d0fde57ab55febb53dd
ruoyi-framework/src/main/java/com/ruoyi/framework/config/ResourcesConfig.java
@@ -1,12 +1,25 @@
package com.ruoyi.framework.config;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.concurrent.TimeUnit;
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.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
@@ -30,9 +43,6 @@
    @Value("${file.path}")
    private String filePath;
    @Value("${outPath}")
    private String outPath;
    @Value("${wordUrl}")
    private String wordUrl;
@@ -49,7 +59,6 @@
        //设置文件虚拟路径映射
        registry.addResourceHandler("/img/**").addResourceLocations("file:" + filePath + "/");
        registry.addResourceHandler("/outPath/**").addResourceLocations("file:" + outPath);
        registry.addResourceHandler("/word/**").addResourceLocations("file:" + wordUrl + "/");
    }
@@ -82,4 +91,30 @@
        // 返回新的CorsFilter
        return new CorsFilter(source);
    }
    // 全局格式化处理
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        String dateFormat = "yyyy-MM-dd HH:mm:ss";
        Jackson2ObjectMapperBuilder json = Jackson2ObjectMapperBuilder.json();
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        //localDateTime格式化
        JavaTimeModule module = new JavaTimeModule();
        LocalDateTimeDeserializer dateTimeDeserializer = new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(dateFormat));
        LocalDateTimeSerializer dateTimeSerializer = new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateFormat));
        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(dateFormat.split(" ")[0]));
        // 设置格式化内容
        converter.setObjectMapper(objectMapper);
        converters.add(0, converter);
    }
}