zoukankan      html  css  js  c++  java
  • spring boot 2.0 WebMvcConfigurerAdapter过时解决方法

    第一种:
    @Configuration
    public class WebAppConfig implements WebMvcConfigurer{
    
        @Bean
        public HandlerInterceptor getLoginInterceptor(){
            return new LoginInterceptor();
        }
    
        @Override
        public void addInterceptors(InterceptorRegistry registry){
            registry.addInterceptor(getLoginInterceptor())
                    .addPathPatterns("/**")
                    .excludePathPatterns("/error")
                    .excludePathPatterns("/static/*");
        }
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry){
            registry.addResourceHandler("/static/**")
                    .addResourceLocations("classpath:/static/");
        }
    
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")//设置允许跨域的路径
                    .allowedOrigins("*")//设置允许跨域请求的域名
                    .allowCredentials(true)//是否允许证书 不再默认开启
                    .allowedMethods("GET", "POST", "PUT", "DELETE")//设置允许的方法
                    .maxAge(3600);//跨域允许时间
        }
    }

    使用这个在升级springboot2.0之后会把Date类型字段自动给转成UTC字符串 如:1990-11-26T16:00:00.000+0000,如果想转成时间戳在application.properties配置文件增加以下配置:

    spring.jackson.serialization.write-dates-as-timestamps=true
    spring.jackson.time-zone=GMT+8
    第二种(会导致springboot的自动配置失效):
    @Configuration
    public class WebAppConfig extends WebMvcConfigurationSupport{
    
            ...
    
    }

    这种方式会把Date类型字段自动给转成时间戳,如果想用UTC字符串,在application.properties配置文件增加以下配置:

    spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
    spring.jackson.time-zone=GMT+8



    Spring Boot2.0的版本(IDEA创建的时候自动选择的这个版本),然后编译器告诉我WebMvcConfigurerAdapter已过时了

    WebMvcConfigurerAdapter源码

    @Deprecated
    public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {
    
        /**
         * {@inheritDoc}
         * <p>This implementation is empty.
         */
        @Override
        public void configurePathMatch(PathMatchConfigurer configurer) {
        }
    

    可以使用以下实现:

    @Configuration
    public class WebMvcConfg implements WebMvcConfigurer {
      //省略
    }
    

    推荐 WebMvcConfigurationSupport

    @Configuration
    public class WebMvcConfg extends WebMvcConfigurationSupport {
      //省略
    }
    
     


     
  • 相关阅读:
    nuget 命令行小技巧
    非静态类、方法、属性要实例化对象
    ViewData 和 ViewBag
    每日记载内容总结32
    每日记载内容总结31
    hibernate 学习知识总结
    spring mvc 页面编码和数据库编码 中文出现乱码
    java代码生成二维码以及解析二维码
    每日记载内容总结30
    换公司,重新开始
  • 原文地址:https://www.cnblogs.com/exmyth/p/10743378.html
Copyright © 2011-2022 走看看