liujie
2026-01-29 b74a483a59ab4925b4f79d2b2e76e17ab4b8177d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.estate.expert.admin.config;
 
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
import javax.annotation.Resource;
import java.util.List;
 
/**
 * 很奇怪,需要自定义一个WebMvcConfig,才能让Jackson2HttpMessageConverter生效
 * https://www.cnblogs.com/joeblackzqq/p/17716810.html
 */
@Slf4j
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
 
    @Resource
    private ObjectMapper objectMapper;
 
    @Value("${file.upload.dir}")
    private String uploadDir;
 
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.removeIf(o -> o instanceof MappingJackson2HttpMessageConverter);
        converters.add(new MappingJackson2HttpMessageConverter(objectMapper));
        converters.add(new ResourceHttpMessageConverter());
    }
 
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置报告文件访问路径
        String reportPath = "file:" + uploadDir + "/reports/";
        registry.addResourceHandler("/files/reports/**")
                .addResourceLocations(reportPath);
 
        // 配置报告图片访问路径
        String imagePath = "file:" + uploadDir + "/reports/images/";
        registry.addResourceHandler("/files/reports/images/**")
                .addResourceLocations(imagePath);
 
        log.info("静态资源映射配置完成:");
        log.info("  - /files/reports/** -> {}", reportPath);
        log.info("  - /files/reports/images/** -> {}", imagePath);
    }
}