This article first appeared personal website: custom URL matching rules of combat Spring Boot
When building web applications, not all of the URL request follows the default rules. Sometimes, when we want RESTful URL matching delimiters comprises, in this case, you may be referred Spring "delimiter format defined by" "."; Sometimes, we want to identify the presence of a slash. Spring provides an interface for developers to customize on demand.
In previous articles may be customized by the filter program WebConfiguration class, formatting tools, etc., have the same, similar approaches can be arranged "path matching rule" used in this class.
Suppose ISBN format allows delimiter. "" Books division number and revision number, of the form [isbn-number]. [Revision ]
Real
- Add WebConfiguration arranged in the corresponding class code as follows:
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.setUseSuffixPatternMatch(false).setUseTrailingSlashMatch(true);
}
- By
mvn spring-boot:run
starting the application - access
http://localhost:8080/books/9781-1234-1111.1
- access
http://localhost:8080/books/9781-1234-1111
analysis
configurePathMatch (PathMatchConfigurer configurer) function allows developers to customize the URL path matching rules based on demand.
- configurer.setUseSuffixPatternMatch (false) represents designers want the system will not recognize the URL of external exposure and match. suffix. In this case, it means that Spring will 9781-1234-1111.1 as a isbn * parameter to BookController.
- configurer.setUseTrailingSlashMatch (true) Indicates whether the system does not distinguish between URL of the last character is a slash /. In this case, it means
http://localhost:8080/books/9781-1234-1111
, andhttp://localhost:8080/books/9781-1234-1111/
have the same meaning.
If you need to customize the process path match occurs, you can provide your own custom PathMatcher and UrlPathHelper , but this demand is not common.
Spring Boot 1.x series
- Spring Boot automatic configuration, Command-line-Runner
- Understanding Automatic configuration of Spring Boot
- Spring Boot of @PropertySource notes in the integration of Redis
- Spring Boot project, how to customize HTTP message converter
- Spring Boot Restful interface to provide integrated Mongodb
- Spring bean in scope
- Spring Boot event dispatcher used in the project mode
- Error handling practices when Spring Boot provides RESTful interface
- Spring Boot of combat customize their starter
- How Spring Boot project supports both HTTP and HTTPS protocols
- How Spring Boot starter customized settings to automatically configure annotation
- Spring Boot used in the project Mockito
- Use Spock testing framework in Spring Boot project
- Spring Boot project, how to customize interceptor
- Spring Boot project, how to customize PropertyEditors
- How Spring Boot project to build a Web form input on the server check
- Spring Boot application health monitoring
- Spring Boot project, how to customize the servlet-filters
This focus on the number of back-end technology, JVM troubleshooting and optimization, Java interview questions, personal growth and self-management, and other topics, providing front-line developers work and growth experience for the reader, you can expect to gain something here.