【Video & Communication Platform】
http://study.163.com/course/introduction.htm?courseId=1004329008&utm_campaign=commission&utm_source=400000000155061&utm_medium=share
http://study.163.com/course/introduction.htm?courseId=1004638001&utm_campaign=commission&utm_source=400000000155061&utm_medium=share
https://gitee.com/happyangellxq520/spring-boot
à Spring Boot communication platform
http://412887952-qq-com.iteye.com/blog/2321532
Origin of need :
In a previous blog, " Spring Boot Custom URL Matching Rules " was asked in NetEase Cloud Classroom ( Portal ): After url customization, inherit WebMvcConfigurationSupport and reconfigurePathMatch , but static resources cannot access 404 . There really is this problem, this blog is to solve this problem.
Let's review the article first: " Spring Boot Custom URL Matching Rules ".
1. Review " Spring Boot Custom URL Matching Rules"
1.1 Origin of demand
The origin of the matter: Some people say that a /hello access path is written, but, no matter if you enter /hello or /hello.html , or /hello.xxx can be accessed. At the time, I thought he processed the code, but later found out that it was not, and later found out that it was a Spring Boot routing rule. Well, there is nonsense, so let's see how we solve the problem caused by the above.
构建web应用程序时,并不是所有的URL请求都遵循默认的规则。有时,我们希望RESTful URL匹配的时候包含定界符“.”,这种情况在Spring中可以称之为“定界符定义的格式”;有时,我们希望识别斜杠的存在。Spring提供了接口供开发人员按照需求定制。
1.2 解决步骤
核心的开发步骤就是两步(这是之前的解决方案):
(1)启动类 extends WebMvcConfigurationSupport
(2)重写configurePathMatch方法
具体实现代码:
package com.kfit; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; /** * * @author Angel --守护天使 * @version v.0.1 * @date 2016年7月29日下午7:06:11 */ @SpringBootApplication public class ApiCoreApp extends WebMvcConfigurationSupport{ /** * 1、 extends WebMvcConfigurationSupport * 2、重写下面方法; * setUseSuffixPatternMatch : 设置是否是后缀模式匹配,如“/user”是否匹配/user.*,默认真即匹配; * setUseTrailingSlashMatch : 设置是否自动后缀路径模式匹配,如“/user”是否匹配“/user/”,默认真即匹配; */ @Override public void configurePathMatch(PathMatchConfigurer configurer) { configurer.setUseSuffixPatternMatch(false) .setUseTrailingSlashMatch(true); } public static void main(String[] args) { SpringApplication.run(ApiCoreApp.class, args); } }
其中访问代码:
@RequestMapping("/user") public String hello(){ return "/user"; }
1.3 代码分析
以上代码有两句核心的代码:
第一句代码:setUseSuffixPatternMatch(boolean useSuffixPatternMatch):
设置是否是后缀模式匹配,如“/user”是否匹配/user.*,默认真即匹配;
(1)当此参数设置为true的时候,那么/user.html,/user.aa,/user.*都能是正常访问的。
(2)当此参数设置为false的时候,那么只能访问/user或者/user/( 这个前提是setUseTrailingSlashMatch 设置为true了)。
第二句代码:setUseTrailingSlashMatch (boolean useSuffixPatternMatch):
设置是否自动后缀路径模式匹配,如“/user”是否匹配“/user/”,默认真即匹配;
(1)当此参数设置为true后,那么地址/user,/user/都能正常访问。
(2)当此参数设置为false的时候,那么就只能访问/user了。
1.4 总结
(1)当以上两个参数都设置为true的时候,那么路径/user或者/user.aa,/user.*,/user/都是能正常访问的,但是类似/user.html/ 是无法访问的。
(2)当都设置为false的时候,那么就只能访问/user路径了。
二、定制URL匹配规则终极篇
2.1 新问题的产生
以为上面的解决了问题,但是没想到出现了新的问题:url订制之后继承 WebMvcConfigurationSupport 重新configurePathMatch,但是静态资源均无法访问404,这是为什么呢?
2.2 问题原因
首先我们要知道:Spring Boot 默认为我们提供了静态资源处理,使用 WebMvcAutoConfiguration 中的配置各种属性。
其次我们要知道:如果想要自己完全控制WebMVC,就需要在@Configuration注解的配置类上增加@EnableWebMvc(@SpringBootApplication 注解的程序入口类已经包含@Configuration),增加该注解以后WebMvcAutoConfiguration中配置就不会生效,你需要自己来配置需要的每一项。
最后要知道:@EnableWebMvc=WebMvcConfigurationSupport,使用了@EnableWebMvc注解等于扩展了WebMvcConfigurationSupport但是没有重写任何方法。
总结:继承了WebMvcConfigurationSupport之后spring boot就使用了我们自己定义的Web MVC规则了,这导致没有建立了静态资源的映射路径,也就导致了静态资源的无法访问。
2.3 问题解决
解决思路很简单,只需要替换一个继承类即可,看如下代码:
原先的为:
@SpringBootApplication public class App extends WebMvcConfigurationSupport
替换为:
@SpringBootApplication public class App extends WebMvcConfigurerAdapter
其它的不需要动,就可正常访问到静态资源了。这是什么道理呢?
2.4 分析
我们先看一下几种使用方式:
(1)@EnableWebMvc+extends WebMvcConfigurationAdapter
在扩展的类中重写父类的方法即可,这种方式会屏蔽Spring Boot的@EnableAutoConfiguration中的设置。
(2)extends WebMvcConfigurationSupport
在扩展的类中重写父类的方法即可,这种方式会屏蔽Spring Boot的@EnableAutoConfiguration中的设置。
(3)extends WebMvcConfigurationAdapter
在扩展的类中重写父类的方法即可,这种方式依旧使用Spring Boot的@EnableAutoConfiguration中的设置
@EnableWebMvc注解的作用: 就是把WebMvcConfigurationSupport中的配置加载过来,你继承WebMvcConfigurerAdapter时自定义的一些配置会覆盖WebMvcConfigurationSupport 里的默认配置。
2.4 总结
我们上面代码使用的是(3)extends WebMvcConfigurationAdapter:这种方式依旧使用了spring boot的@EnableAutoConfiguration的设置,也就是@EnableWebMvc的配置也是生效的,那么WebMvcConfigurationSupport中的配置就会被加载进来,而我们继承的WebMvcConfigurerAdapter覆盖的方法又会覆盖WebMvcConfigurationSupport里的默认配置。好了,本篇文章就到这里,希望你能有所收获。