WebMvcConfigurerAdapter、WebMvcConfigurer、WebMvcConfigurationSupport、WebMvcAutoConfiguration

1 简介

WebMvcConfigurerAdapter、WebMvcConfigurer、WebMvcConfigurationSupport、WebMvcAutoConfiguration
首先以上4个类都是springboot中mvc的支持类

2 使用

2.1 WebMvcConfigurer

WebMvcConfigurer 为接口

2.2 WebMvcConfigurerAdapter

WebMvcConfigurerAdapter 是 WebMvcConfigurer 的实现类大部分为空方法 ;是WebMvcConfigurer的子类实现,由于Java8中可以使用default关键字为接口添加默认方法,为在源代码中spring5.0之后就已经弃用本类。如果需要我接着可以实现WebMvcConfigurer类

2.3 WebMvcConfigurationSupport

WebMvcConfigurationSupport 是mvc的基本实现并包含了WebMvcConfigurer接口中的方法

2.4 WebMvcAutoConfiguration

WebMvcAutoConfiguration 是mvc的自动装在类并部分包含了WebMvcConfigurer接口中的方法,继承WebMvcConfigurationSupport或者@EnableWebMvc 会导致WebMvcAutoConfiguration不加载,所以默认配置丢失导致静态资源等无法访问; 需要@Override addResourceHandlers

如果在springboot项目中没有使用到以上类,那么会自动启用WebMvcAutoConfiguration类做自动加载; 项目中的配置都是默认的,比如静态资源文件的访问

3 拦截器

添加自己的拦截器addInterceptors:
方案一:
继承WebMvcConfigurationSupport 会导致WebMvcAutoConfiguration不加载,所以默认配置丢失导致静态资源等无法访问; 需要@Override addResourceHandlers

方案二
实现继承WebMvcConfigurer因为是接口实现所以WebMvcAutoConfiguration还会默认配置,所以静态资源是可以访问的

4 方法

4.1 addInterceptors:拦截器

/** 添加拦截器 **/
void addInterceptors(InterceptorRegistry registry);

addInterceptor:需要一个实现HandlerInterceptor接口的拦截器实例
addPathPatterns:用于设置拦截器的过滤路径规则
excludePathPatterns:用于设置不需要拦截的过滤规则

@Override
public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/toLogin","/login");
        super.addInterceptors(registry);
}

4.2 addCorsMappings:跨域

/** 解决跨域问题 **/
public void addCorsMappings(CorsRegistry registry) ;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedOrigins("*").allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE").maxAge(3600).allowCredentials(true);
    }

4.3 addViewControllers:跳转指定页面

/** 视图跳转控制器 **/
void addViewControllers(ViewControllerRegistry registry);

    /**
     * 以前要访问一个页面需要先创建个Controller控制类,在写方法跳转到页面
     * 在这里配置后就不需要那么麻烦了,直接访问http://localhost:8080/toLogin就跳转到login.html页面了
     *
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/toLogin").setViewName("login");
        registry.addViewController("/").setViewName("/index");
        registry.addViewController("/login").setViewName("forward:/index.html");
        super.addViewControllers(registry);
    }

4.4 视图配置

/** 这里配置视图解析器 **/
void configureViewResolvers(ViewResolverRegistry registry);

/**

  • 视图配置
  • @param registry
    */
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
    super.configureViewResolvers(registry);
    registry.viewResolver(resourceViewResolver());
    /registry.jsp(“/WEB-INF/jsp/”,“.jsp”);/
    }

/**

  • 配置请求视图映射
  • @return
    */
    @Bean
    public InternalResourceViewResolver resourceViewResolver()
    {
    InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
    //请求视图文件的前缀地址
    internalResourceViewResolver.setPrefix(“/WEB-INF/jsp/”);
    //请求视图文件的后缀
    internalResourceViewResolver.setSuffix(“.jsp”);
    return internalResourceViewResolver;
    }

4.5 addResourceHandlers:静态资源

/** 静态资源处理 **/
void addResourceHandlers(ResourceHandlerRegistry registry);

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //自定义项目内目录
        //registry.addResourceHandler("/my/**").addResourceLocations("classpath:/my/");
        //指向外部目录
        registry.addResourceHandler("/my/**").addResourceLocations("file:E:/my/");
        super.addResourceHandlers(registry);
    }

configureMessageConverters:信息转换器param converters

@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);

}

/** 配置内容裁决的一些选项 **/
void configureContentNegotiation(ContentNegotiationConfigurer configurer);

/** 默认静态资源处理器 **/
void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer);

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值