在spring中配置webMvc有两种方法,一种是继承WebMvcConfigurationSupport,另一种方式就是继承WebMvcConfigurer,但是要多加一个@EnableWebMvc注解。相比来说继承WebMvcConfigurer更安全些,因为里面都是空方法。
在实际项目中最常用的就是增加自定义拦截器
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
//添加自定义拦截器
registry.addInterceptor();
}
}
为什么要加@EnableWebMvc注解呢?
因为@EnableWebMvc注解类上导入了DelegatingWebMvcConfiguration类,该类是WebMvcConfigurationSupport的子类,该类会实例化WebMvcConfigurationSupport,另一个作用就是收集BeanFactory中所有WebMvcConfigurer实现,汇集到WebMvcConfigurerComposite中,在WebMvcConfigurationSupport实例化过程中会分别调用这些实现,将相应的实例传入这些实现中,供开发者在此基础上添加自定义的配置。这也就是在WebMvcConfigurer子类上加@EnableWebMvc的原因,因为要先实例化WebMvcConfigurationSupport。
注意:
对于springboot来讲,继承WebMvcConfigure无需再加@EnableWebMvc注解,因为springboot已经实例化了WebMvcConfigurationSupport,如果添加该注解,默认的WebMvcConfigurationSupport配置类是不会生效的,会以用户自定义的为主,一般建议还是不覆盖默认比较好。同时,继承WebMvcConfigurationSupport后,springboot的WebMvc自动配置失效(有可能导致视图解析器无法正常工作)。
在实际使用中也长使用这两个类来实现跨域的配置
例如在继承WebMvcConfigurationSupport后重写 public void addCorsMappings(CorsRegistry registry)
方法来实现跨域
示例代码
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new ByteArrayHttpMessageConverter());
converters.add(new StringHttpMessageConverter());
converters.add(new ResourceHttpMessageConverter());
converters.add(new AllEncompassingFormHttpMessageConverter());
converters.add(new StringHttpMessageConverter());
converters.add(jackson2HttpMessageConverter());
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
/** 本地文件上传路径 */
registry.addResourceHandler(Constants.RESOURCE_PREFIX + "/**").addResourceLocations("file:" + RuoYiConfig.getProfile() + "/");
/** swagger配置 */
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
/**
* long类型转String(解决浏览器Long类型精度丢失问题)
*
* @return 值
*/
@Bean
public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
// Long类型转String类型
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
mapper.registerModule(simpleModule);
converter.setObjectMapper(mapper);
//返回值MULL的问题
// mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
return converter;
}
@Override
public void addCorsMappings(CorsRegistry registry) {
// 设置允许跨域的路径
registry.addMapping("/**")
// 设置允许跨域请求的域名
.allowedOrigins("*")
.exposedHeaders("Authorization")
// 是否允许证书
.allowCredentials(true)
// 设置允许的方法
.allowedMethods("GET", "POST", "DELETE", "PUT")
// 设置允许的header属性
.allowedHeaders("*")
// 跨域允许时间
.maxAge(3600);
}
}
实现 WebMvcConfigurer 中的addCorsMappings方法来实现跨域
后端配置跨域的实现方式有很多,主要是通过拦截器拦截,由于springboot自己集成的工具配置类也有很多已经集成好了相关类,比如只要实现WebMvcConfigurer其接口或者重写WebMvcConfigurationSupport函数就能被springboot框架自动加入到过滤器中