标签:Springboot springframework 详解 registry org WebMvcConfigurer import annotation
用途:跨域、拦截器、静态资源处理
接口方法的作用:
addInterceptors:拦截器 addViewControllers:页面跳转 addResourceHandlers:静态资源 configureDefaultServletHandling:默认静态资源处理器 configureViewResolvers:视图解析器 configureContentNegotiation:配置内容裁决的一些参数 addCorsMappings:跨域 configureMessageConverters:信息转换器
在Spring Boot 1.5版本都是靠重写WebMvcConfigurerAdapter的方法来添加自定义拦截器,消息转换器等。SpringBoot 2.0 后,该类被标记为@Deprecated(弃用)。官方推荐直接实现WebMvcConfigurer或者直接继承WebMvcConfigurationSupport,方式一实现 WebMvcConfigurer接口(推荐),方式二继承WebMvcConfigurationSupport类,具体实现可看这篇文章。https://blog.csdn.net/fmwind/article/details/82832758
package com.olive.config; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * WebMvcConfigurer * */ @Configuration @EnableWebMvc public class ConfigurerAdapter implements WebMvcConfigurer { //图片保存路径 public static final String PIC_PATH = "/landscape/"; @Value(value="${application.profile}") private String profile; //跨域 @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowCredentials(true) .allowedHeaders("*") .allowedOrigins("*") .allowedMethods("GET","POST","PUT","DELETE"); } // 可解决Long 类型在 前端精度丢失的问题, 如不想全局 直接添加注解 @JsonSerialize(using= ToStringSerializer.class) 到相应的字段 @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/").setCachePeriod(0); /** 图片传路径 */ registry.addResourceHandler("/landscape/**").addResourceLocations("file:" + profile); }
// 添加拦截器 @Override public void addInterceptors(InterceptorRegistry registry) { /*拦截所有url请求*/ registry.addInterceptor(authenticationInterceptor()) .addPathPatterns("/**"); } }
标签:Springboot,springframework,详解,registry,org,WebMvcConfigurer,import,annotation 来源: https://www.cnblogs.com/zhougongjin/p/15880762.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。