package org.jeecg.config; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.actuate.trace.http.InMemoryHttpTraceRepository; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * Spring Boot 2.0 解决跨域问题 * * @Author qinfeng * */ @Configuration public class WebMvcConfiguration implements WebMvcConfigurer { @Value("${jeecg.path.upload}") private String upLoadPath; @Value("${jeecg.path.webapp}") private String webAppPath; @Value("${spring.resource.static-locations}") private String staticLocations; /** * 静态资源的配置 - 使得可以从磁盘中读取 Html、图片、视频、音频等 */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**") .addResourceLocations("file:" + upLoadPath + "//", "file:" + webAppPath + "//") .addResourceLocations(staticLocations.split(",")); } /** * 方案一: 默认访问根路径跳转 doc.html页面 (swagger文档页面) * 方案二: 访问根路径改成跳转 index.html页面 (简化部署方案: 可以把前端打包直接放到项目的 webapp,上面的配置) */ @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("doc.html"); } @Bean @Conditional(CorsFilterCondition.class) public CorsFilter corsFilter() { final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource(); final CorsConfiguration corsConfiguration = new CorsConfiguration(); //是否允许请求带有验证信息 corsConfiguration.setAllowCredentials(true); // 允许访问的客户端域名 corsConfiguration.addAllowedOrigin("*"); // 允许服务端访问的客户端请求头 corsConfiguration.addAllowedHeader("*"); // 允许访问的方法名,GET POST等 corsConfiguration.addAllowedMethod("*"); urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration); return new CorsFilter(urlBasedCorsConfigurationSource); } /** * 序列换成json时,将所有的long变成string * js中long过长精度丢失 */ @Bean @Primary @ConditionalOnMissingBean(ObjectMapper.class) public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) { ObjectMapper objectMapper = new ObjectMapper(); SimpleModule simpleModule = new SimpleModule(); //忽略在json字符串中存在,在java类中不存在字段,防止错误。 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true); simpleModule.addSerializer(Long.class, ToStringSerializer.instance); simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance); objectMapper.registerModule(simpleModule); return objectMapper; } /** * SpringBootAdmin的Httptrace不见了 * https://blog.csdn.net/u013810234/article/details/110097201 */ @Bean public InMemoryHttpTraceRepository getInMemoryHttpTrace(){ return new InMemoryHttpTraceRepository(); } }