Request mapping for Springboot

Posted by rolajaz on Tue, 28 Dec 2021 21:32:58 +0100

@GetMapping, @ PostMapping, @ PutMapping, etc.: they are equivalent to RequestMapping(value = "/ xxx", method = RequestMethod.GET) or RequestMapping(value = "/ xxx", method = RequestMethod.POST), that is, the abbreviation of the qualified request method.

Request mapping principle

What is request mapping?
Personal understanding: first, search a URL in the browser and send a request to the program. The program will process it and find the corresponding page. Or send a post or get request to the program when the form is submitted.

  • Let's see how requests are processed in Springboot:
  • Spring MVC functional analysis is from org springframework. web. servlet. Dispatcherservlet - < doDispatch() (that is, it is finally processed by the doDispatch function)

    In spring boot, all requests come to the dispatcher servlet. From this figure, we can see that the inheritance tree comes to the HttpServlet. Anyway, we can finally find the doDispatch method.
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HttpServletRequest processedRequest = request;
		HandlerExecutionChain mappedHandler = null;
		boolean multipartRequestParsed = false;

		WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);

		try {
			ModelAndView mv = null;
			Exception dispatchException = null;

			try {
				processedRequest = checkMultipart(request);
				multipartRequestParsed = (processedRequest != request);

				// Find which Handler (Controller's method) is used to process the current request
				mappedHandler = getHandler(processedRequest);
                
                //Handler mapping: processor mapping/ xxx->>xxxx
               // HandlerMapping means processor mapping. It assigns a URL to a Controller. The function of HandlerMapping is to resolve the request link, and then find the class executing the request according to the request link (the handler in HandlerMapping is the Controller or Action we write)

As can be seen from the above code, the appropriate Handler (Controller's method) will be found through the getHandler method. It is to see whether the request corresponds to @ RequestMapping. If so, the @ RequestMapping bound method (handle) will be returned. As shown in the following figure

As can be seen from the following figure, there are five values in handlerMapping:

RequestMappingHandlerMapping, which ranks first, saves the mapping rules of @ RequestMapping and handler in all controllers. From the following figure, you can see @ RequestMapping on the left and the corresponding method bound in the Controller, handel, on the right

All request mappings are in HandlerMapping.

  • SpringBoot automatically configures the welcome pagehandlermapping of the welcome page. Access / be able to access index html;
  • SpringBoot automatically configures the default RequestMappingHandlerMapping

First, the request comes in and tries all the HandlerMapping one by one to see if there is any request information. If so, find the handler corresponding to the request. If not, it is the next HandlerMapping.

How the welcome page works:
If you don't pass anything, that is, "/", then the appropriate one will not be found in RequestMappingHandlerMapping, and then it will cycle to the next controller, welcome page handlermapping. The controller is dedicated to processing "/", so it is forwarded to index. Com according to the processing HTML.

Use and principle of rest

  • Previous: / getUser get user / deleteUser delete user / editUser modify user / saveUser save user
  • Now: / user GET - get user DELETE - DELETE user PUT - modify user POST - save user.

To use the rest style, you need to manually enable it in the configuration file (it seems that the new version is enabled by default)

mvc:
     hiddenmethod:
		filter:
			enabled: true

Core Filter; HiddenHttpMethodFilter

  • Usage: form method=post, hide field_ method=put
    The form only has get and post. It uses the rest style and takes put and delete requests as hidden requests of post requests.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1> Hello!!!!</h1>

<form action="/user" method="get">
    <input value="Rest_GET Submit" type="submit"/>
</form>

<form action="/user" method="post">
    <input value="Rest_POST Submit" type="submit"/>
</form>

<form action="/user" method="post">
    <input name="_method" type="hidden" value="delete"/>
    <input value="Rest_delete Submit" type="submit"/>
</form>

<form action="/user" method="post">
    <input name="_method" type="hidden" value="PUT"/>
    <input value="Rest_Put Submit" type="submit"/>
</form>

</body>
</html>
package com.atguigu.boot.ccontroller;

import org.springframework.web.bind.annotation.*;

/**
 * @author Juneluo
 * @create 2021-08-15 20:52
 */

@RestController
public class HelloController {
    @RequestMapping("/user")
//    @RequestMapping(value = "/user",method = RequestMethod.GET)
    public String getUser(){
        return "GET-Zhang San";
    }

    @PostMapping("/user")
//    @RequestMapping(value = "/user",method = RequestMethod.POST)
    public String saveUser(){
        return "POST-Zhang San";
    }

    @PutMapping("/user")
//    @RequestMapping(value = "/user",method = RequestMethod.PUT)
    public String putUser(){
        return "PUT-Zhang San";
    }

    @DeleteMapping("/user")
//    @RequestMapping(value = "/user",method = RequestMethod.DELETE)
    public String deleteUser(){
        return "DELETE-Zhang San";
    }


}

Topics: Python Java Web Development Spring Spring Boot