Spring MVC core classes and annotations – @RequestMapping annotation (3) Request mapping method

1. Classification of request mapping methods

Based on the annotation style Spring MVC, the URL path of the request mapping is specified through the @RequestMapping annotation. Commonly used methods of URL path mapping include request-based URL path mapping, Ant-style URL path mapping, and REST-style URL path mapping. Next, these three request mapping methods will be explained in detail.

a. URL path mapping based on request method

In addition to using the @RequestMapping annotation to limit the client's request method, starting from Spring 4.3, you can also use combined annotations to limit the client's request method. Combined annotations simplify the mapping of common HTTP request methods and better express the semantics of the annotated methods.

2. Spring MVC combination annotations

@GetMapping: Matches GET request.

@PostMapping: Match POST request.

@PutMapping: Match PUT request.

@DeleteMapping: Match DELETE request.

@PatchMapping: Match requests in PATCH mode.

3. @GetMapping usage example

Next, take @GetMapping as an example to explain the usage of combined annotations. @GetMapping is the abbreviation of @RequestMapping(method = RequestMethod.GET). Using combined annotations instead of @RequestMapping annotations can omit the method attribute, thereby simplifying the code. @GetMapping usage sample code is shown below.

@GetMapping(value="/firstController")
public void sayHello(){
    ...
}

b. Ant-style URL path mapping

Spring MVC supports Ant-style URL path mapping. The so-called Ant style is actually a wildcard style. Wildcards can be used in the processor mapping path to associate accessed URL paths. There are three types of Ant-style wildcards, namely: ? matches any single character; * matches 0 or any number of characters; ** matches 0 or multi-level directories.

4. Ant-style wildcard path matching

wildcard

URL path

Wildcard matching instructions

?

/ant1?

Matches the /ant1[anyone] path under the project root path, where [anyone] can be any single character, that is, there is only one character after /ant1. Such as /ant12, /ant1a.

*

/ant2/*.do

Match the /ant2/[any].do path under the project root path, where [any] can be any number of characters. Such as /ant2/findAll.do, /ant2/.do.

*

/*/ant3

Matches the /[onemore]/ant3 path under the project root path, where [onemore] can be any character greater than 0. Such as /a/ant3, /findAll/ant3, but the number of characters cannot be 0, and the number of directory levels must be the same, such as //ant3, /findAll/a/ant3.

**

/**/ant4

Matches the /[anypath]/ant4 path under the project root path, where [anypath] can be a directory with 0 or more levels. Such as /ant4, /a/ant4, /a/b/ant4.

**

/ant5/**

Matches the /ant5/[anypath] path under the project root path, where [anypath] can be 0 or multi-level directories. Such as /ant5, /ant5/a, /ant5/a/b.

5. Mapping paths using multiple wildcards

When multiple wildcard characters are used in the mapping path at the same time, wildcard conflicts may occur. When multiple wildcards conflict, the path will follow the longest matching principle (has more characters) to match the wildcard. If a request path satisfies two or more Ant-style mapping path matching rules at the same time, then the request path will eventually match the matching rules. The path with the most characters. For example, /ant/a/path satisfies both /**/path and /ant/*/path matching rules, but /ant/path will eventually match the “/ant/*/path” path.

c. URL path mapping based on RESTful style

RESTful is to access network resources according to the REST style. Simply put, RESTful is a style that turns request parameters into request paths. REST (Representational State Transfer) is a network resource access style that standardizes the access method of network resources. The network resource accessed by REST can be a piece of text, a song, a service, or in short, a specific existence. Each network resource has a URI pointing to it. To obtain this resource, just access its URI. Therefore, the URI is the unique identifier of each resource.

6. The difference between traditional style and RESTful style access URL formats

The URL format for traditional style access is as follows.

http://.../findUserById?id=1

After adopting the RESTful style, the URL format it accesses is as follows.

http://.../user/id/1 

It should be noted that URLs in the RESTful style do not use paths in the form of verbs. For example, findUserById means querying the user, which is a verb, while user means the user, which is a noun.

7. Basic request operations in RESTful style

In the RESTful style, in the HTTP request, the four verbs GET, POST, PUT and DELETE correspond to four basic request operations, as shown below.

•GET is used to obtain resources

•POST is used to create new resources

•PUT is used to update resources

•DELETE is used to delete resources

8. Conventional methods of four requests in RESTful style

URL path

Request method

illustrate

http://localhost:8080/chapter11/user/1

HTTP GET

Get parameter 1 to query user operation

http://localhost:8080/chapter11/user/1

HTTP DELETE

Get parameter 1 to query user operation

http://localhost:8080/chapter11/user/1

HTTP PUT

Get parameter 1 to query user operation

http://localhost:8080/chapter11/user

HTTP POST

Add user operation

9. Advantages of using RESTful style

Conventions are not specifications, conventions can be broken, so it is called RESTful style, not RESTful specification. The advantage of using the RESTful style is that the writing of the path is relatively simple, and it is impossible to know what kind of operation is done through the address, and the access behavior of the resource can be hidden.