package com.saye.hrs.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import java.nio.charset.Charset; import java.util.List; /** * Description: 配置虚拟路径 * @author dqzhang * @created 2019年11月19日 下午3:26:06 */ @Configuration public class VirtualPathConfig extends WebMvcConfigurationSupport{ @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { // TODO Auto-generated method stub // registry.addResourceHandler("/images/**").addResourceLocations("file:d:/saye/"); registry.addResourceHandler("/**").addResourceLocations("classpath:/static/"); registry.addResourceHandler("/webjars/**").addResourceLocations( "classpath:/META-INF/resources/webjars/"); super.addResourceHandlers(registry); } @Override protected void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowedMethods("GET", "HEAD", "POST","PUT", "DELETE", "OPTIONS") .allowedHeaders("*") .exposedHeaders("access-control-allow-headers", "access-control-allow-methods", "access-control-allow-origin", "access-control-max-age", "X-Frame-Options") .allowCredentials(false).maxAge(3600); super.addCorsMappings(registry); } @Bean public HttpMessageConverter responseBodyConverter() { return new StringHttpMessageConverter(Charset.forName("UTF-8")); } @Override public void configureMessageConverters(List> converters) { converters.add(responseBodyConverter()); // 这里必须加上加载默认转换器,不然bug玩死人,并且该bug目前在网络上似乎没有解决方案 // 百度,谷歌,各大论坛等。你可以试试去掉。 addDefaultHttpMessageConverters(converters); } @Override public void configureContentNegotiation(ContentNegotiationConfigurer configurer) { configurer.favorPathExtension(false); } }