Java超详细讲解WebMvcConfigurer拦截器

2022-11-13 10:11:59 拦截器 讲解 详细

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内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关文章