package cn.wisenergy.chnmuseum.party.conf; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; /** * MVC配置 */ @Configuration public class MvcConfiguration extends WebMvcConfigurationSupport { /** * 后置跨域支持【当出现跨域请求,此处会放在拦截器最后执行,CORS失效】 * * @param registry */ @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOriginPatterns("*") .allowedHeaders("*") .allowedMethods("*") .allowCredentials(true) .maxAge(3600); } /** * 配置消息转换器:Ali开源的fastJson * * @param converters */ // @Override // public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { // // 先移除jackson转换器,springBoot1.x可以不排除 // for (int i = converters.size() - 1; i >= 0; i--) { // if (converters.get(i) instanceof MappingJackson2HttpMessageConverter) { // converters.remove(i); // } // } // //1.需要定义一个convert转换消息的对象; // FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); // StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(); // ByteArrayHttpMessageConverter byteArrayHttpMessageConverter = new ByteArrayHttpMessageConverter(); // // //2.添加fastJson的配置信息,比如:是否要格式化返回的json数据; // FastJsonConfig fastJsonConfig = new FastJsonConfig(); // fastJsonConfig.setSerializerFeatures( // SerializerFeature.PrettyFormat, // SerializerFeature.WriteMapNullValue, // SerializerFeature.WriteNullStringAsEmpty, // SerializerFeature.DisableCircularReferenceDetect, // SerializerFeature.WriteNullListAsEmpty, // SerializerFeature.BrowserCompatible, // SerializerFeature.WriteDateUseDateFormat); // // 设置编码 // fastJsonConfig.setCharset(Charset.forName("UTF-8")); // fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss"); // // // 设置数字转化问题 // SerializeConfig serializeConfig = SerializeConfig.globalInstance; // serializeConfig.put(BigInteger.class, ToStringSerializer.instance); // serializeConfig.put(Long.class, ToStringSerializer.instance); // serializeConfig.put(Long.TYPE, ToStringSerializer.instance); // serializeConfig.setPropertyNamingStrategy( PropertyNamingStrategy.CamelCase); // fastJsonConfig.setSerializeConfig(serializeConfig); // // //3处理中文乱码问题 // List<MediaType> fastMediaTypes = new ArrayList<>(); // fastMediaTypes.add(MediaType.APPLICATION_JSON); // fastMediaTypes.add(MediaType.TEXT_HTML); // fastMediaTypes.add(MediaType.MULTIPART_FORM_DATA); // // //4.在convert中添加配置信息. // fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes); // fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); // // //5.将convert添加到converters当中. // converters.add(fastJsonHttpMessageConverter); // converters.add(stringHttpMessageConverter); // converters.add(byteArrayHttpMessageConverter); // } /** * 启用@EnableWebMvc后,properties文件中的静态路径失效,必须覆盖后重新制定 * 配置静态访问资源 * * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { // swagger2配置 registry.addResourceHandler("/swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); // 静态资源拦截 registry.addResourceHandler("/**") .addResourceLocations("classpath:/META-INF/") .addResourceLocations("classpath:/META-INF/resources/") .addResourceLocations("classpath:/resources/") .addResourceLocations("classpath:/static/") .addResourceLocations("classpath:/public/") .addResourceLocations("classpath:/statics/") .addResourceLocations("classpath:/template/") .addResourceLocations("classpath:/"); } }