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 {
      //省略
    }
    
     


     
  • 相关阅读:
    TSQL常用查询语句
    团队开发注意事项
    SQLServer2005 XML数据类型操作
    Nunit学习笔记
    服务器架构工具表
    JQuery 做的下拉文本框
    面向对象和结构化程序设计的区别
    AFX_MSG是什么意思
    DECLARE_MESSAGE_MAP()
    AfxBeginThread的介绍/基本用法
  • 原文地址:https://www.cnblogs.com/exmyth/p/10743378.html
Copyright © 2011-2022 走看看