Function and introduction of RequestMapping annotation in spring MVC

Posted by nonexist on Sun, 02 Jan 2022 13:05:46 +0100

Source code:

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
    String name() default "";

    @AliasFor("path")
    String[] value() default {};

    @AliasFor("value")
    String[] path() default {};

    RequestMethod[] method() default {};

    String[] params() default {};

    String[] headers() default {};

    String[] consumes() default {};

    String[] produces() default {};
}

Introduction to RequestMapping

@The main function of the RequestMapping annotation is to identify the mapping relationship between the request path and the method, as shown in the following case:

@RequestMapping(path="/hello")
public String sayHello() {
    System.out.println("Hello SpringMVC!!");
    return "success";
}

The value of the @ RequestMapping annotation in the above case is / hello on the sayHello() method. Represents a user request http://localhost:8080/hello The request will be intercepted and processed by the sayHello() method.

RequestMapping action location

This annotation can act on methods and classes.

Function on class: first level access directory

Function on method: second level access directory

The following cases:

@Controller
@RequestMapping(value = "/hello")
public class HelloController {

    /**
     * User access http://localhost:8080/hello/say , leave it to the sayHello() method
     * Receive request
     * @return
     */
    @RequestMapping(value="/say")
    public String sayHello() {
        System.out.println("Hello SpringMVC!!");
        return "success";
    }
}

The requested address changes

http://localhost:8080/hello/say

Properties of RequestMapping

path

Specifies the url of the request path

Add @ RequestMapping(path="/hello") to the HelloController method and @ RequestMapping(path="/say") to the sayHello method. At this time, users can access it directly http://localhost:8080/hello/say To access the method

@Controller
@RequestMapping(path = "/hello")
public class HelloController {

    /**
     * User access http://localhost:8080/hello/say , leave it to the sayHello() method
     * Receive request
     * @return
     */
    @RequestMapping(path="/say")
    public String sayHello() {
        System.out.println("Hello SpringMVC!!");
        return "success";
    }
}

value

The value attribute is the same as the path attribute

Replace the path in the above method with value, and the user can access it directly http://localhost:8080/hello/say To access the method.

@Controller
@RequestMapping(value = "/hello")
public class HelloController {

    /**
     * User access http://localhost:8085/hello/say , leave it to the sayHello() method
     * Receive request
     * @return
     */
    @RequestMapping(value="/say")
    public String sayHello() {
        System.out.println("Hello SpringMVC!!");
        return "success";
    }
}

method

We have written two methods, sayHello() and sayyourname (), but they have the same request path, but the value of the method attribute is different. If it is requestmethod GET, you can only request in GET mode. If it is requestmethod POST can only be requested in POST mode.

@Controller
@RequestMapping(value = "/hello")
public class HelloController {
    /**
     * Added method = requestmethod GET
     * At this time, this method only receives http://localhost:8080/hello/say GET submit mode request
     * @return
     */
    @RequestMapping(value = "/say",method = RequestMethod.GET)
    public String sayHello() {
        System.out.println("Hello SpringMVC!!Called GET method");
        return "success";
    }

    /**
     * Added method = requestmethod POST
     * At this time, this method only receives http://localhost:8080/hello/say POST submission mode request for
     * @return
     */
    @RequestMapping(value = "/say",method = RequestMethod.POST)
    public String sayYourName() {
        System.out.println("Hello SpringMVC!!Called POST method");
        return "success";
    }
}

Params (a qualification)

Specify the conditions that limit the request parameters
param1: indicates that the request must contain a request parameter named param1
! param1: indicates that the request cannot contain a request parameter named param1
param1 != value1: indicates that the request contains a request parameter named Param1, but its value cannot be value1
Request must have two parameters named param1 and param2 = param1}

/**
 * params Parameters:
 *      "name":Indicates that the request must contain the name parameter
 *      "!age": Indicates that the request cannot contain an age parameter
 *      "address!=usa": Indicates that the value of the address parameter in the request cannot be usa
 *      "working=sz": Indicates that the working parameter in the request parameter must be sz
* @return
*/
@RequestMapping(params = {"name","!age","address!=usa","working=sz"},
                    value = "/say",method = RequestMethod.GET)
public String sayHello() {
    System.out.println("Hello SpringMVC!!Called GET method");
    return "success";
}

Headers the request header (a qualification) that must be included in the request sent by headers


param1: indicates that the request header must contain the request header information named param1
    ! param1: indicates that the request header cannot contain the request header information named param1
    param1 != value1: indicates that the request header contains request header information named Param1, but its value cannot be value1
{"param1=value1", "param2"}: the request header must contain two request header information of header information param1 and param2, and the value of param1 must be value1

/**
 * headers Parameters:
 *      "Accept": Indicates that the request header must contain the Accept header information
 *      "!Date":Indicates that the request header cannot contain Date header information
 *      "Accept-Encoding!=zh-CN":The value indicating the accept encoding header information in the request header cannot be zh CN
 *      "Host=localhost:18081":Indicates that the value of Host in the request header must be localhost:18081
 *
 * @return
 */
@RequestMapping(headers = {"Accept","!Date","Accept-Encoding!=zh-CN","Host=localhost:18081"},
                value = "/say",method = RequestMethod.GET)
public String sayHello() {
    System.out.println("Hello SpringMVC!!Called GET method");
    return "success";
}

Topics: Java Maven Spring Spring Cloud server