Java超详细讲解WebMvcConfigurer拦截器
WebMvcConfigurer接口常用的方法:
void addInterceptors(InterceptorRegistry var1);
void addViewControllers(ViewControllerRegistry registry);
void addResourceHandlers(ResourceHandlerRegistry registry);
void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer);
void configureViewResolvers(ViewResolverRegistry registry);
void configureContentNegotiation(ContentNegotiationConfigurer configurer);
public void addCorsMappings(CorsRegistry registry) ;
addInterceptors拦截器
addInterceptor:需要一个实现HandlerInterceptor接口的拦截器实例
addPathPatterns:用于设置拦截器的过滤路径规则;addPathPatterns(“ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/my @Bean public InternalResourceViewResolver resourceViewResolver() { InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver(); //请求视图文件的前缀地址 internalResourceViewResolver.setPrefix("/WEB-INF/jsp/"); //请求视图文件的后缀 internalResourceViewResolver.setSuffix(".jsp"); return internalResourceViewResolver; } @Override public void configureViewResolvers(ViewResolverRegistry registry) { super.configureViewResolvers(registry); registry.viewResolver(resourceViewResolver()); }
addCorsMappings跨域
@Override
public void addCorsMappings(CorsRegistry registry) {
super.addCorsMappings(registry);
registry.addMapping("/cors
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//调用父类的配置
super.configureMessageConverters(converters);
//创建fastJson消息转换器
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//创建配置类
FastJsonConfig fastJsonConfig = new FastJsonConfig();
//修改配置返回内容的过滤
fastJsonConfig.setSerializerFeatures(
SerializerFeature.DisableCircularReferenceDetect,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullStringAsEmpty
);
fastConverter.setFastJsonConfig(fastJsonConfig);
//将fastjson添加到视图消息转换器列表内
converters.add(fastConverter);
}
到此这篇关于Java超详细讲解WebMvcConfigurer拦截器的文章就介绍到这了,更多相关Java WebMvcConfigurer内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!