Spring Boot 定制URL匹配规则的方法

事情的起源:有人问我,说编写了一个/hello访问路径,但是吧,不管是输入/hello还是/hello.html,还是/hello.xxx都能进行访问。当时我还以为他对代码进行处理了,后来发现不是,后来发现这是Spring Boot路由规则。好了,有废话了下,那么看看我们解决上面这个导致的问题。

构建web应用程序时,并不是所有的URL请求都遵循默认的规则。有时,我们希望RESTful URL匹配的时候包含定界符“.”,这种情况在Spring中可以称之为“定界符定义的格式”;有时,我们希望识别斜杠的存在。Spring提供了接口供开发人员按照需求定制。

核心的开发步骤就是两步:

(1)启动类 extends WebMvcConfigurationSupport

(2)重写configurePathMatch方法;

具体实现代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
  publicvoid configurePathMatch(PathMatchConfigurer configurer) {
    configurer.setUseSuffixPatternMatch(false)
          .setUseTrailingSlashMatch(true);
  }
   
  publicstaticvoid main(String[] args) {
    SpringApplication.run(ApiCoreApp.class, args);
  }
}

其中访问代码:

1
2
3
4
@RequestMapping("/user")
public String hello(){
  return"/user";
}

以上代码有两句核心的代码:

setUseSuffixPatternMatch(boolean useSuffixPatternMatch):

设置是否是后缀模式匹配,如“/user”是否匹配/user.*,默认真即匹配;

当此参数设置为true的时候,那么/user.html,/user.aa,/user.*都能是正常访问的。

当此参数设置为false的时候,那么只能访问/user或者/user/( 这个前提是setUseTrailingSlashMatch 设置为true了)。

setUseTrailingSlashMatch (boolean useSuffixPatternMatch):

设置是否自动后缀路径模式匹配,如“/user”是否匹配“/user/”,默认真即匹配;

当此参数设置为true的会后,那么地址/user,/user/都能正常访问。

当此参数设置为false的时候,那么就只能访问/user了。

当以上两个参数都设置为true的时候,那么路径/user或者/user.aa,/user.*,/user/都是能正常访问的,但是类似/user.html/ 是无法访问的。

当都设置为false的时候,那么就只能访问/user路径了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

原文链接:http://412887952-qq-com.iteye.com/blog/2315107

http://www.jb51.net/article/106715.htm

其中DefaultMockMvcBuilder还提供了如下API:

addFilters(Filter... filters)/addFilter(Filter filter, String... urlPatterns):添加javax.servlet.Filter过滤器

defaultRequest(RequestBuilder requestBuilder):默认的RequestBuilder,每次执行时会合并到自定义的RequestBuilder中,即提供公共请求数据的;

alwaysExpect(ResultMatcher resultMatcher):定义全局的结果验证器,即每次执行请求时都进行验证的规则;

alwaysDo(ResultHandler resultHandler):定义全局结果处理器,即每次请求时都进行结果处理;

dispatchOptions:DispatcherServlet是否分发OPTIONS请求方法到控制器;

 

StandaloneMockMvcBuilder继承了DefaultMockMvcBuilder,又提供了如下API:

setMessageConverters(HttpMessageConverter<?>...messageConverters):设置HTTP消息转换器;

setValidator(Validator validator):设置验证器;

setConversionService(FormattingConversionService conversionService):设置转换服务;

addInterceptors(HandlerInterceptor... interceptors)/addMappedInterceptors(String[] pathPatterns, HandlerInterceptor... interceptors):添加spring mvc拦截器;

setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager):设置内容协商管理器;

setAsyncRequestTimeout(long timeout):设置异步超时时间;

setCustomArgumentResolvers(HandlerMethodArgumentResolver... argumentResolvers):设置自定义控制器方法参数解析器;

setCustomReturnValueHandlers(HandlerMethodReturnValueHandler... handlers):设置自定义控制器方法返回值处理器;

setHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers)/setHandlerExceptionResolvers(HandlerExceptionResolver... exceptionResolvers):设置异常解析器;

setViewResolvers(ViewResolver...resolvers):设置视图解析器;

setSingleView(View view):设置单个视图,即视图解析时总是解析到这一个(仅适用于只有一个视图的情况);

setLocaleResolver(LocaleResolver localeResolver):设置Local解析器;

setFlashMapManager(FlashMapManager flashMapManager):设置FlashMapManager,如存储重定向数据;

setUseSuffixPatternMatch(boolean useSuffixPatternMatch):设置是否是后缀模式匹配,如“/user”是否匹配"/user.*",默认真即匹配;

setUseTrailingSlashPatternMatch(boolean useTrailingSlashPatternMatch):设置是否自动后缀路径模式匹配,如“/user”是否匹配“/user/”,默认真即匹配;

addPlaceHolderValue(String name, String value) :添加request mapping中的占位符替代;

 

因为StandaloneMockMvcBuilder不会加载Spring MVC配置文件,因此就不会注册我们需要的一些组件,因此就提供了如上API用于注册我们需要的相应组件。

http://jinnianshilongnian.iteye.com/blog/2004660

 

posted @ 2017-07-10 22:22  沧海一滴  阅读(26904)  评论(0编辑  收藏  举报