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> converters) { converters.removeIf(c -> c instanceof MappingJackson2HttpMessageConverter); converters.add(new MappingJackson2HttpMessageConverter(jacksonObjectMapperCustomization())); } }