Commonly used annotations

  • Controller

Annotate a class that represents a controller, and Spring MVC automatically scans the class that annotates the annotation.

  • RequestMapping

Request path mapping, which can annotate classes or methods, can specify the request type, not all by default.

  • RequestParam

It is placed before a parameter, which means that only data in a= B format can be received, that is, content-Type is Application/X-www-form-urlencoded Content.

  • RequestBody

It is placed before the parameter to indicate that the parameter is fetched from the request body, not from the address bar, so it must be a non-A = B format for receiving a POST request, i.e. content-Type is not application/ X-www-form-urlencoded.

  • ResponseBody

Before a method or return type, to indicate that the data returned by the method is placed in the Response Body instead of the jump page. Typically used for Ajax requests that return JSON data.

  • RestController

This is a Controller and ResponseBody annotation, indicating that all of the return parameters from the @controller class are in response Body.

  • PathVariable

Path binding variable, used to bind variables on restful paths.

  • @RequestHeader

Used to retrieve the parameter values in the Request header before the method parameters.

  • @CookieValue;

Used to retrieve the parameter values in the Request Header cookie before the method parameters.

  • GetMapping PostMapping PutMapping.. *Mapping is a new annotation added in Spring4.3 to indicate a specific request type path Mapping instead of writing RequestMethod to specify the request type.

demo

import org.dom4j.util.UserDataElement;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/test")
public class TestController {

    @RequestMapping(value = "/get/{no}", method = RequestMethod.GET)
    @ResponseBody
    public Object get(@PathVariable("no") String no) {
        return new UserDataElement("");
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public void save(@RequestBody UserDataElement user) {

    }

}
Copy the code

Recommended reading

Dry goods: 2TB architect four-stage video tutorial

Interview: the most complete Java multithreaded interview questions and answers

Interview: the most comprehensive ali advanced Java interview questions in history

Interview: The most complete Spring interview questions in history

Tutorial: The most complete Spring Boot complete video tutorial

Books: 15 must-read books for advanced Java architects

Tools: Recommended an online creation flow chart, mind mapping software

Share Java dry goods, high concurrency programming, hot technology tutorials, microservices and distributed technology, architecture design, blockchain technology, artificial intelligence, big data, Java interview questions, and cutting-edge hot news.