Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of Spring5 path matcher PathPattern

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "what is the use of Spring5 path matcher PathPattern", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what is the use of Spring5 path matcher PathPattern" this article.

Spring5 path matcher PathPattern

PathPattern handles url address matching faster, and the main differences between it and AntPathMatcher are as follows:

1.PathPattern only supports the use of * * at the end

If you use * * in the middle of the path, an error will be reported.

@ GetMapping ("/ funyi/**") public String act1 () {return "/ funyi/**";} 2.PathPattern supports the use of such as {* path}

At the same time, you can match to the multi-level path, and assign the obtained value to the formal parameter path of the corresponding controller method

@ GetMapping ("/ funyi/ {* path}") public void act2 (@ PathVariable String path) {System.out.println ("path =" + path);}

Add the following configuration to the SpringBoot project to enable PathPattern:

@ Configurationpublic class WebConfig implements WebMvcConfigurer {@ Override public void configurePathMatch (PathMatchConfigurer configurer) {configurer.setPatternParser (new PathPatternParser ());}} path matching tool (AntPathMatcher vs PathPattern)

The first version of AntPathMatcher:Sping (2013 pronunciation) was introduced.

PathPattern:Spring 5 is introduced in the package: org.springframework.web.util.pattern.PathPattern, and the module is spring-web. It can be seen that it is a "tool" designed for Web.

PathPattern removes the word Ant, but maintains good downward compatibility: except that it does not support writing * * in the middle of path (to disambiguate), all other matching rules are consistent with AntPathMatcher in behavior, and powerful {* pathVariable} support has been added. On the whole, it can be considered that the latter is compatible with the functions of the former.

The performance of PathPattern is better than AntPathMatcher. Theoretically, the more complex pattern is, the more obvious the advantage of PathPattern is.

AntPathMatcher can be used in non-Web environments, while PathPattern is only available in Web environments. So PathPattern is not a complete substitute for AntPathMatcher.

In terms of internal implementation principle, AntPathMatcher performs pure string operation and comparison, while PathPattern for any string pattern will eventually be parsed into several segments of PathElement, these PathElement are chained together to represent the pattern to form an object data, this structured representation makes it more readable and flexible, thus achieving better performance.

Examples of simple use of both:

New AntPathMatcher () .match ("/ api/v1/**", "/ api/v1/2/3**"); new PathPatternParser () .parse ("/ api/v1/**") .requests (PathContainer.parsePath ("/ api/v1/2/3**")); / / each pathPattern string corresponds to a PathPatternParser, and each parsedPath string corresponds to a PathContainer

Some friends may say: I can use PathPattern objects normally in the Service layer, even the Dao layer. Why?

This problem is equivalent to: HttpServletRequest is a dedicated component of the web layer, but you can still pass it to the Service layer or even the Dao layer for use without reporting errors when compiling and running.

The above is all the content of the article "what is the use of Spring5 path matcher PathPattern". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report