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


     
  • 相关阅读:
    servlet-servletConfig
    servlet-servletContext网站计数器
    servlet-cookie
    Android 无cp命令 mv引起cross-device link
    android使用mount挂载/system/app为读写权限,删除或替换系统应用
    android使用百度地图、定位SDK实现地图和定位功能!(最新、可用+吐槽)
    解决android sdk manager无法下载SDK 的问题
    Android APK反编译详解(附图)
    Android如何防止apk程序被反编译
    不用外部JAR包,自己实现JSP文件上传!
  • 原文地址:https://www.cnblogs.com/exmyth/p/10743378.html
Copyright © 2011-2022 走看看