whyclxw
2025-06-12 eb81b0a16b2d4e87eefd085231ab6b20a1ea3cde
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.whyc.config;
 
import com.fasterxml.jackson.databind.ObjectMapper;
import com.whyc.constant.YamlProperties;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.context.annotation.Bean;
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.*;
 
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.TimeZone;
 
/**
 * @Description : static resources Config
 * @date 2020/09/15
 **/
@Configuration
@EnableWebMvc
public class StaticResourceConfig extends WebMvcConfigurerAdapter {
 
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
 
        //这个是可行的,解析的时候path为*.html,校验路径admin下是否存在
        registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/");
 
        //项目jar同级目录下,图片能通过http方式访问到,很重要!
        ApplicationHome applicationHome = new ApplicationHome(getClass());
        File jarFile = applicationHome.getDir();
        //在jar包所在目录下生成一个upload文件夹用来存储上传的图片
 
        /**文档存储路径*/
        String baseDirPath;
 
 
        if(YamlProperties.runModel == 1) {
            //开发路径
            baseDirPath = jarFile.getParentFile().toString()+File.separator+"pis_file"+File.separator;
        }else {
            //打包路径
            baseDirPath = jarFile.toString()+File.separator+"pis_file"+File.separator;
        }
        registry.addResourceHandler("/pis_file/**").addResourceLocations("file:/"+baseDirPath);
 
        super.addResourceHandlers(registry);
 
    }
 
    @Bean
    public ObjectMapper jacksonObjectMapperCustomization() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        TimeZone timeZone = TimeZone.getTimeZone("Asia/Shanghai");
        format.setTimeZone(timeZone);
 
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
                .timeZone(timeZone)
                .dateFormat(format);
 
        return builder.build();
    }
 
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.removeIf(c -> c instanceof MappingJackson2HttpMessageConverter);
        converters.add(new MappingJackson2HttpMessageConverter(jacksonObjectMapperCustomization()));
    }
 
}