| | |
| | | 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; |
| | |
| | | * 很奇怪,需要自定义一个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); |
| | | } |
| | | } |