Spring Boot combat the custom URL matching rules

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:runstarting the application
  • accesshttp://localhost:8080/books/9781-1234-1111.1

When path matching, pattern matching without using the suffix (*)

  • accesshttp://localhost:8080/books/9781-1234-1111

Use the correct URL to access the results

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, and http://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

  1. Spring Boot automatic configuration, Command-line-Runner
  2. Understanding Automatic configuration of Spring Boot
  3. Spring Boot of @PropertySource notes in the integration of Redis
  4. Spring Boot project, how to customize HTTP message converter
  5. Spring Boot Restful interface to provide integrated Mongodb
  6. Spring bean in scope
  7. Spring Boot event dispatcher used in the project mode
  8. Error handling practices when Spring Boot provides RESTful interface
  9. Spring Boot of combat customize their starter
  10. How Spring Boot project supports both HTTP and HTTPS protocols
  11. How Spring Boot starter customized settings to automatically configure annotation
  12. Spring Boot used in the project Mockito
  13. Use Spock testing framework in Spring Boot project
  14. Spring Boot project, how to customize interceptor
  15. Spring Boot project, how to customize PropertyEditors
  16. How Spring Boot project to build a Web form input on the server check
  17. Spring Boot application health monitoring
  18. 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.
Jwadu

Guess you like

Origin www.cnblogs.com/javaadu/p/11828184.html