In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
WebFlux in the Path parameter resolution and url mapping is how, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, hope you can get something.
Path Parameter Analysis and url Mapping of WebFlux
Asynchronous, reactive and functional programming has gradually become mainstream recently. Spring5 has increased its support for reactive programming through Reactor, while Spring WebFlux is different from the previous web framework. As a non-blocking asynchronous web framework, it can make full use of multi-core CPU hardware resources and provide stronger concurrency support. Spring official support for WebFlux is very friendly, basically for java developers accustomed to Spring WEB, it can be easily migrated.
Next, we will enter the WebFlux series of tutorials, and strive to use the most concise language to introduce the basic ways to play WebFlux, so that you can smoothly switch and use WebFlux to experience the charm of reactive programming.
The editor will mainly introduce the url matching when WebFlux provides web interface and the corresponding path parameter parsing.
i. Project environment
This project is developed with the help of SpringBoot 2.2.1.RELEASE + maven 3.5.3 + IDEA
1. Dependence
Using WebFlux, the main introduction dependencies are as follows (omitting the related dependencies of SpringBoot, for example, if you are not clear about how to create a SpringBoot project, you can follow my previous blog post)
Org.springframework.boot spring-boot-starter-webflux II. Path matching and parameter parsing
All of the following are based on the official documentation: https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-ann-requestmapping-uri-templates
The following example is mainly based on annotations, and there is not much difference between basic knowledge points and SpringWeb (as for the use of functions, it will be discussed later)
1. Basic path parameter acquisition
Path parameters, for example, name and test in http://127.0.0.1:8080/name/test are path parameters, which are mainly obtained by @ PathVariable
A concrete example
@ RestController@RequestMapping (path = "path") public class PathAction {/ * the most basic path acquisition method * * @ param index * @ return * / @ GetMapping (path = "/ basic/ {index}") public Mono basic (@ PathVariable (name = "index") int index) {return Mono.just ("path index:" + index);}}
For the above case, we simply design three access case, and the specific results are as follows
➜~ curl 'http://127.0.0.1:8080/path/basic/1'path index: 1%➜ ~ curl' http://127.0.0.1:8080/path/basic/1/2'{"timestamp":"2020-08-26T13:35:26.221+0000","path":"/path/basic/1/2","status":404,"error":"Not Found "," message ": null "requestId": "8256bf73"}% ➜~ curl 'http://127.0.0.1:8080/path/basic/'{"timestamp":"2020-08-26T13:35:32.196+0000","path":"/path/basic/","status":404,"error":"Not Found "," message ": null," requestId ":" eeda1111 "}%
Please note that in the output above, / basic/ {index} can only match single-level path path parameters, and in the above writing, this level of path path must exist
If you look at the PathVariable annotation, you can see that there is a required property in it. What happens if it is set to false?
@ GetMapping (path = "/ basic2/ {index}") public Mono basic2 (@ PathVariable (name = "index", required = false) Integer index) {return Mono.just ("basic2 index:" + index);}
The test case is as follows
➜~ curl 'http://127.0.0.1:8080/path/basic2/'{"timestamp":"2020-08-26T13:41:40.100+0000","path":"/path/basic2/","status":404,"error":"Not Found "," message ": null "requestId": "b2729e2c"}% ➜~ curl 'http://127.0.0.1:8080/path/basic2/22'basic2 index: 22%➜ ~ curl' http://127.0.0.1:8080/path/basic2/22/3'{"timestamp":"2020-08-26T13:41:44.400+0000","path":"/path/basic2/22/3","status":404,"error":"Not Found "," message ": null," requestId ":" 0b3f173c "}%
From the actual case above, you can also see that the level property is set to false, but the url path still needs to be matched correctly, not even one more level or less.
two。 Multiple path parameters
There is only one path parameter above. If there are more than one parameter, it is relatively simple.
/ * param index * @ param order * @ return * / @ GetMapping (path = "/ mbasic/ {index} / {order}") public Mono mbasic (@ PathVariable (name = "index") int index, @ PathVariable (name = "order") String order) {return Mono.just ("mpath arguments:" + index + "|" + order);}
The test case is as follows
➜~ curl 'http://127.0.0.1:8080/path/mbasic/1/asc'mpath arguments: 1 | asc%3. Partial path parameter matching
The above two case completely match a certain level of path. Here is a partial match of the case.
Part of the content in the path matches * *-/ part/test.txt-> name = test *-/ part/a/test.txt-> does not match * * @ param name * @ return * / @ GetMapping (path = "/ part/ {name} .txt") public Mono part (@ PathVariable (name = "name") String name) {return Mono.just ("part path argument:" + name);}
Please note that the path path above is suffixed with .txt, such as hello in part/hello.txt in the following example
➜~ curl 'http://127.0.0.1:8080/path/part/hello.txt'part path argument: hello%➜ ~ curl' http://127.0.0.1:8080/path/part/hello.tx'{"timestamp":"2020-08-26T13:47:49.121+0000","path":"/path/part/hello.tx","status":404,"error":"Not Found "," message ": null," requestId ":" 1075d683 "}% 4. Regular matching
Next comes the higher-end path parameter matching, which supports some simple regularities. For example, we want to parse the path path of spring-web-3.0.5.jar and use spring-web as name and 3.0.5 as version,.jar as ext.
So our rest interface can be written as follows
/ * * regular match * * / path/path/pattern/spring-web-3.0.5.jar-> name = spring-web, version=3.0.5, ext=.jar * * @ return * / @ GetMapping (path = "/ pattern/ {name: [Amurz -] +}-{version:\\ d\.\ d\.\.\ d} {ext:\. [amurz] +}") public Mono urlPattern (@ PathVariable (name = "name") String name @ PathVariable (name= "version") String version, @ PathVariable (name= "ext") String ext) {return Mono.just ("pattern arguments name=" + name + "version=" + version + "ext=" + ext) } 5. Multi-level path parameter matching
Note that all the above words have a feature, that is, full / partial matching can only be performed for single-level path paths (the middle / / between path paths is used as the first level in this article), so what can I do if I want my path parameters to match multiple levels?
For example, in the / path/name/hello request path, I want to use / name/hello as a path parameter
For the above scenario, we mainly deal with it with the help of {* name}. Pay attention to the * sign before the parameter name.
/ * match: * *-/ path/pattern2-> name = = "" *-/ path/pattern2/hello-> name = = / hello *-/ path/pattern2/test/hello-> name = / test/hello * * @ param name * @ return * / @ GetMapping (path = "/ pattern2/ {* name}") public Mono pattern2 (@ PathVariable (name = "name") String name) {return Mono.just ("pattern2 argument:"+ name);}
The test case is as follows
➜~ curl 'http://127.0.0.1:8080/path/pattern2'pattern2 argument:%➜ ~ curl' http://127.0.0.1:8080/path/pattern2/hello'pattern2 argument: / hello% ➜~ curl 'http://127.0.0.1:8080/path/pattern2/hello/world'pattern2 argument: / hello/world%6. Path matching
The previous introduction is the parsing of path parameters, so let's take a brief look at the three most common path matching methods.
A. *
An asterisk that matches 0 or1 and single-level path paths
/ * * single * number can only match the first-level directory. Note the difference between this method and the pattern2 above * * can match: * *-/ path/pattern3/hello *-/ path/pattern3 * * does not match * *-/ path/pattern3/hello/1 * * @ return * / @ GetMapping (path = "/ pattern3/*") public Mono pattern3 () {return Mono.just ("pattern3 succeed!");}
The measured case is as follows
# Please note Here is the ➜~ curl 'http://127.0.0.1:8080/path/pattern3'{"timestamp":"2020-08-27T00:01:20.703+0000","path":"/path/pattern3","status":404,"error":"Not Found "," message ": null without / ending "requestId": "c88f5066"}% ➜~ curl 'http://127.0.0.1:8080/path/pattern3/'pattern3 succeeded%➜ ~ curl' http://127.0.0.1:8080/path/pattern3/a'pattern3 succeeded% ➜~ curl 'http://127.0.0.1:8080/path/pattern3/a/b'{"timestamp":"2020-08-27T00:01:18.144+0000","path":"/path/pattern3/a/b", "status": 404, "error": "Not Found", "message": null, "requestId": "203dc7d4"}%
Please note the example above. / path/pattern3 accesses 404, while / path/pattern3/ is allowed. The only difference is that there is an extra suffix /.
Why?
Is it because there is an asterisk in front of the path path?
Next, let's design a case, kill / in front of *, and test it again.
@ GetMapping (path = "/ pattern33**") public Mono pattern33 () {return Mono.just ("pattern33 succeed!");}
Test again, and the results are as follows
➜~ curl 'http://127.0.0.1:8080/path/pattern3311'pattern33 succeeded%➜ ~ curl' http://127.0.0.1:8080/path/pattern33/11'{"timestamp":"2020-08-27T00:05:51.236+0000","path":"/path/pattern33/11","status":404,"error":"Not Found "," message ": null "requestId": "d8cbd546"}% ➜~ curl 'http://127.0.0.1:8080/path/pattern33'pattern33 succeeded%➜ ~ curl' http://127.0.0.1:8080/path/pattern331/'pattern33 succeeded%
With the help of the first two case, we can basically see the role of *
* the front is an exact match
For example, / pattern3/*, the path path accessed must be prefixed with / pattern3/.
* at most represents a single-level path, which simply means that / x cannot appear in the location represented by *
For example, / pattern33**, then / pattern331/ can match, but / pattern331/1 cannot.
B. *
Different from the single * above, it matches the 0-1 level path path, while two * * means that it can match all the way to the last layer.
/ * all those starting with pattern4 can match * * @ return * / @ GetMapping (path = "/ pattern4/**") public Mono pattern4 () {return Mono.just ("pattern4 succeed!");}
The test case is as follows
➜~ curl 'http://127.0.0.1:8080/path/pattern4'pattern4 succeeded%➜ ~ curl' http://127.0.0.1:8080/path/pattern4/12'pattern4 succeeded% ➜~ curl 'http://127.0.0.1:8080/path/pattern4/12/3'pattern4 succeeded%
Attention please
Direct access to / pattern4 can also be hit, which is different from the above.
C.?
The wildcard of a single character is relatively simple as follows
/ * matches pattern5/test pattern5/tast... * does not match pattern5/tst pattern5/tesst * * @ return * / @ GetMapping (path = "/ pattern5/t?st") public Mono pattern5 () {return Mono.just ("pattern5 succeed!");}
Visit case
➜~ curl 'http://127.0.0.1:8080/path/pattern5/test'pattern5 succeeded%➜ ~ curl' http://127.0.0.1:8080/path/pattern5/t/st'{"timestamp":"2020-08-27T00:13:42.557+0000","path":"/path/pattern5/t/st","status":404,"error":"Not Found "," message ": null "requestId": "add34639"}% ➜~ curl 'http://127.0.0.1:8080/path/pattern5/tst'{"timestamp":"2020-08-27T00:14:01.078+0000","path":"/path/pattern5/tst","status":404,"error":"Not Found "," message ": null," requestId ":" b2691121 "}%
It can also be seen from the test output above
? The corresponding place cannot be / and other unsupported characters (such as?,', ",%, etc.)
? The corresponding place must exist.
7. Summary
Although the theme of this article is that the parsing of path parameters in webflux matches the mapping of url, we will find miraculously that these knowledge points seem to be no different from those in SpringMVC, and in fact this is true; for the use of annotations, most of them are how to play before, and how to play now.
Here is a table to summarize the knowledge points above.
Example of pattern description? Match one character pages/t?st.html match / pages/test.html and / pages/t3st.html* match single-level path path 0-multiple characters "/ resources/*.png" matches "/ resources/file.png" / projects/*/versions "matches" / projects/spring/versions "but does not match" / projects/spring/boot/versions "* * match 0-multiple path paths" / resources/** "matches" / resources/file.png " And "/ resources/images/file.png" and "/ resources/**/file.png" is illegal {name} matches single-stage path path parameters "/ projects/ {project} / versions" matches "/ projects/spring/versions" and captures project=spring {name: [Amurz] +} regular "/ projects/ {project: [Amurz] +} / versions" matches "/ projects/spring/versions" but not "/ projects/spring1/versions" {* path} match path path 0-the last level path path parameter "/ resources/ {* file}" matches "/ resources/images/file.png" and captures file=images/file.png is helpful to you after reading the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.