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 matching principle of URL @ PathVariable variables?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what is the matching principle of URL @ PathVariable variables". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the matching principle of URL @ PathVariable variables"?

Variable matching principle of URL @ PathVariable matching principle with variables in url

In setting the path of url, we may use variables to improve the flexibility of the path, such as

@ RequestMapping (value= "/ {str} / qian", method=RequestMethod.GET) @ ResponseBody public String qianStr (@ PathVariable String str) {return "qianStr:" + str;}

Then you can match the qianStr method when entering the http://localhost:8080/zhende/qian path, but you haven't thought about exactly how to achieve the match.

At first, you can predict how to match the path through regular expressions, but the question is whether I entered / zhende/qian to match / {str} / qian, but I still can't figure it out, so write a demo to debug to see how to match. The process is as follows:

Demo

Use springboot to write a simple web

Package com.example.demo;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;@Controller () @ RequestMapping ("/ get") public class TestController {@ RequestMapping (value= "/ li", method = RequestMethod.GET) @ ResponseBody public String test () {return "test" } @ RequestMapping (value= "/ {str}", method=RequestMethod.GET) @ ResponseBody public String li (@ PathVariable String str) {return "li:" + str;} @ RequestMapping (value= "/ qian/ {str}", method=RequestMethod.GET) @ ResponseBody public String qianStr (@ PathVariable String str) {return "qianStr:" + str } @ RequestMapping (value= "/ {str} / qian", method=RequestMethod.GET) @ ResponseBody public String strQian (@ PathVariable String str) {return "strQian:" + str;}}

Several paths are written on it so that we can better match the matching process.

Debug as follows

After starting the service, enter the following path:

You can see that there is a DispatcherServlet dispatch call in the call stack on the left, so see that the DispatcherServlet associated code is handled by mappedHandler, so add a breakpoint to DispatcherServlet and debug again.

Enter the method

It is found that there are 5 processing types in handlerMappings. Enter the mapping.getHandler (request) method to view the specific execution process.

The handler obtained in SimpleUrlHandlerMapping is empty, continue to try the second handlermapping type

This method matches the request path in request with the mappings that exists in the service, that is, the formal path matching process. Continue to enter the method

PatternsRequestCondition handles the matching between the pattern in the service and the request lookupPath. If the path is the same, the pattern is returned directly. If the path is different, the path matching continues to be processed through AntPathMatcher.

Match the pattern participle in the service with the requested path, find that the matching of / test/li and / get/zhende/qian failed, and then continue the matching of the next pattern. In the face of the matching of {str} after the participle, the {str} will be converted to (. *) for regular matching, that is, the matching of variables in the url path will be completed.

After finding the path, get the corresponding handlermethod, and then execute the corresponding target path method

Summary

The matching of URL @ PathVariable variables is done by PatternsRequestCondition, specifically handled by AntPathMatcher. After the project starts, the provided url will be put into the container handlerMappings. When the request comes, the request path and the pattern in the handlerMappings will be matched one by one. When matching, the path will be segmented first, and the {str} variable will be converted to (. *) and then matched. After the matching is completed, the corresponding handlermethod execution method will be called to enter the target method.

Remarks

What happens when there are multiple paths in the project that can be matched? Why are there 5 types of handlerMappings in the initial DispatcherServlet?

@ PathVariable@PathVariable maps placeholders bound by URL

Placeholder URL is a new feature of Spring3.0, which is a milestone in the development of SpringMVC towards the goal of REST.

The placeholder parameter in URL can be bound to the input parameter of the controller processing method through @ PathVariable: the {xxx} placeholder in URL can be bound to the input parameter of the action method through @ PathVariable ("xxx").

Example:

SpringMVCTest.java

/ / @ PathVariable can be used to map placeholders in URL to @ RequestMapping ("/ testPathVariable/ {id}") public String testPathVariable (@ PathVariable ("id") Integer id) {System.out.println ("testPathVariable:" + id); return SUCCESS;} in the parameters of the target method

Index.jsp

TestPathVariableREST

REST: Representational State Transfer. (resource) the state transformation of the presentation layer. It is the most popular Internet software architecture at present. It is clear in structure, up to standard, easy to understand and easy to expand, so it is being adopted by more and more websites.

Resources: an entity on a network, or a specific information on the network. It can be a piece of text, a picture, a song, a service, in short, a concrete existence. You can point to it with a URI (uniform resource locator), and each resource corresponds to a specific URI. To get this resource, you can access its URI, so URI is a unique identifier for each resource.

Presentation layer (Representation): the form in which a resource is presented is called its presentation layer (Representation). For example, text can be represented in txt format, HTML format, XML format, JSON format, or even binary format.

State transition (State Transfer): each request made represents an interaction between the client and the server. HTTP protocol is a stateless protocol, that is, all states are stored on the server side. Therefore, if the client wants to operate the server, it must have some means to make the server-side "State Transfer". This kind of transformation is based on the presentation layer, so it is "the state transformation of the presentation layer". Specifically, there are four verbs in the HTTP protocol that denote the mode of operation: GET, POST, PUT, and DELETE. They correspond to four basic operations: GET to obtain resources, POST to create new resources, PUT to update resources, and DELETE to delete resources.

Example:

-/ order/1 HTTP GET: get the order with id = 1

-/ order/1 HTTP DELETE: delete order with id = 1

-/ order/1 HTTP PUT: update order with id = 1

-/ order HTTP POST: add order

HiddenHttpMethodFilter: browser form forms only support GET and POST requests, while DELETE, PUT and other method do not. Spring3.0 adds a filter to convert these requests into standard http methods to support GET, POST, PUT, and DELETE requests.

Configure HiddenHttpMethodFilter in web.xml

Web.xml

HiddenHttpMethodFilter org.springframework.web.filter.HiddenHttpMethodFilter HiddenHttpMethodFilter / *

Test the corresponding method in SpringMVCTest

SpringMVCTest.java

/ * * how to send PUT and DELETE requests * 1. HiddenHttpMethodFilter * 2 needs to be configured. Need to send POST request * 3. Send a POST request with a hidden field of name= "_ method" with a value of DELETE or PUT * * get the id value through @ PathVariable annotation in the springmvc framework * * / / get request @ RequestMapping (value= "/ testRest/ {id}", method=RequestMethod.GET) public String testRest (@ PathVariable Integer id) {System.out.println ("testRest Get" + id); return SUCCESS / / post request @ RequestMapping (value= "/ testRest", method=RequestMethod.POST) public String testRest () {System.out.println ("testRest POST"); return SUCCESS;} / / delete request @ RequestMapping (value= "/ testRest/ {id}", method=RequestMethod.DELETE) public String testRestDelete (@ PathVariable Integer id) {System.out.println ("testRest DELETE" + id); return SUCCESS } / / delete request @ RequestMapping (value= "/ testRest/ {id}", method=RequestMethod.PUT) public String testRestPut (@ PathVariable Integer id) {System.out.println ("testRest PUT" + id); return SUCCESS;}

The way of writing in index.jsp

Index.jsp

Test Rest Get so far, I believe you have a deeper understanding of "what is the matching principle of URL @ PathVariable variables". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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