@RequestMapping and @requestparam annotations

Source: Internet
Author: User

@RequestMapping annotations

@RequestMapping is one of the most commonly used annotations in Spring WEB applications. This annotation maps HTTP requests to the processing methods of the MVC and REST controllers. This annotation can be used not only on methods, but also on classes, which represent all responses to requests in a class by using that address as the parent path, such as the ability to add a virtual directory or something.

code example:

package org.zero01.test;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("/test")public class Test {    @RequestMapping("/test.do")    public void method(){        System.out.println("This is a test output");    }}

At this point, you need to access the method methods in the controller, you need to add/test this virtual directory, for example:

Http://localhost:8090/test/test.do

The following is a brief introduction to the properties in the @requestmapping annotation:

The 1.value is the same as the Path property, which specifies the actual mapped address of the HTTP request resource (URI), and when no specific property value is specified, the default is to pass the value to the property, as an example:

package org.zero01.test;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("/test")public class Test {    @RequestMapping(value = "/value.do")    public void value(){        System.out.println("value");    }    @RequestMapping(path = "/path.do")    public void path(){        System.out.println("path");    }    @RequestMapping("/method.do")    public void method(){        System.out.println("method");    }}

At this point, you can access the following addresses:

Http://localhost:8090/test/value.do
Http://localhost:8090/test/path.do
Http://localhost:8090/test/method.do

Both of these properties can be configured with multiple URI addresses, example:

package org.zero01.test;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("/test")public class Test {    @RequestMapping(value = {"","/view.do","index*","/**/jsp.do","/value.do"})    public void value(){        System.out.println("value");    }}

The 2.consumes property, which specifies the type of submission (Content-type) for which the request is processed, such as Application/json, text/html, etc., example:

package org.zero01.test;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("/test")public class Test {    // 只处理Content-Type的值为application/json的请求    @RequestMapping(value = "json.do", consumes = "application/json")    public void json(){        System.out.println("application/json");    }    // 只处理Content-Type的值为text/html的请求    @RequestMapping(path = "/xml.do", consumes = "text/html")    public void xml() {        System.out.println("text/html");    }}

We can test it by postman:

Console Print Results:

application/jsontext/html

The 3.produces property, which is used to specify the type of content returned, is returned only if the type in the request header (Accept) contains the specified type, as an example:

package org.zero01.test;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("/test")public class Test {    // 该方法仅处理request请求中Accept头中包含了"application/json"的请求,同时暗示了返回的内容类型为application/json;    @RequestMapping(value = "json.do", produces = "application/json")    public void json(){        System.out.println("application/json");    }    // 该方法仅处理request请求中Accept头中包含了"text/xml"的请求,同时暗示了返回的内容类型为text/xml;    @RequestMapping(path = "/xml.do", produces = "text/html")    public void xml() {        System.out.println("text/html");    }}

You can also use postman for testing:

Console Print Results:

application/jsontext/html

The 4.method attribute, which can be seen from the name, is used to specify the type of method requested, such as GET, POST, PUT, delete, and so on, for example:

 package org.zero01.test;import org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.RequestMethod; @Controller @requestmapping ("/test") public class Test {@    Requestmapping (value = "Get.do", method = requestmethod.get) public void Get () {System.out.println ("get"); } @RequestMapping (value = "Post.do", method = requestmethod.post) public void post () {System.out.println ("PO    St "); } @RequestMapping (value = "Delete.do", method = requestmethod.delete) public void Delete () {System.out.print    ln ("delete"); } @RequestMapping (value = "Put.do", method = requestmethod.put) public void put () {System.out.println ("put")    ; } @RequestMapping (value = "Patch.do", method = requestmethod.patch) public void patch () {System.out.println (    "Patch"); }}

The 5.params property, which is used to specify that some parameters in the request are a specific value before the method is processed, for example:

package org.zero01.test;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("/test")public class Test {    // 当user参数为test时执行这个方法    @RequestMapping(value = "test.do", params = "user=test")    public void user(String user) {        System.out.println(user);    }    // 当user参数为test以及alias参数为test时执行这个方法    @RequestMapping(value = "test.do", params = {"user=test","alias=test"})    public void userAndAlias(String user, String alias) {        System.out.println(user);        System.out.println(alias);    }}

Note: In Springmvc, the parameters in request are automatically passed to the corresponding method parameters.

The 6.headers property, which specifies that the request must contain some specified header values for the method to process requests, example:

package org.zero01.test;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("/test")public class Test {    @RequestMapping(value = "test.do", headers = "Referer=http://www.xxx.com/")    public void test() {        System.out.println("test");    }}
@RequestParam annotations

The @RequestParam annotation is simple, it is used to map the request parameter area data to the parameters of the function processing method, since SpringMVC4.2, there are 4 parameters inside the @RequestParam annotation:

    • String Name
    • String value
    • Boolean Required
    • String DefaultValue

1.name with the Value property, where name and value are the name of the URL parameter, which is the same, I personally prefer the name, because in the HTML form we use the Name property to set the URL parameter names, So it is more intuitive to use the name attribute on annotations. Example:

package org.zero01.test;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;@Controller@RequestMapping("/test")public class Test {    @RequestMapping("test.do")    // 指定将username参数的值传递到该方法的name参数上    public void test(@RequestParam(name = "username") String name) {        System.out.println(name);    }    @RequestMapping("user.do")    // 指定将username参数的值传递到该方法的user参数上,alias参数的值则传递到该方法的a参数上    public void userAndAlias(@RequestParam(name = "username")String user, @RequestParam(name = "alias")String a) {        System.out.println(user);        System.out.println(a);    }}

The 2.required property, which is used to specify whether a parameter is required, the default value is True, indicating that there must be a corresponding parameter in the request, otherwise a 404 error code is reported, example:

package org.zero01.test;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;@Controller@RequestMapping("/test")public class Test {    @RequestMapping("test.do")    // 指定username参数是必须的,如果url上没有的话就会报错    public void test(@RequestParam(name = "username", required = true) String name) {        System.out.println(name);    }    @RequestMapping("user.do")    // 指定username与alias参数不是必须的,即便url上没有也不会报错    public void userAndAlias(@RequestParam(name = "username", required = false) String user, @RequestParam(name = "alias", required = false) String a) {        System.out.println(user);        System.out.println(a);    }}

A 3.defaultValue property that specifies the default value of the parameter, indicating that the default value can be a spel expression, such as "#{systemproperties[' java.vm.version '}" if there is no default value for the parameter with the same name in the request. Example:

package org.zero01.test;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;@Controller@RequestMapping("/test")public class Test {    @RequestMapping("test.do")    // url上没有username参数时,给它设置一个默认值为nothing    public void test(@RequestParam(name = "username", defaultValue = "nothing") String name) {        System.out.println(name);    }    @RequestMapping("user.do")    // url上没有username以及alias参数时,给它设置一个默认值为nothing    public void userAndAlias(@RequestParam(name = "username", defaultValue = "nothing") String user, @RequestParam(name = "alias", defaultValue = "nothing") String a) {        System.out.println(user);        System.out.println(a);    }}

@RequestMapping and @requestparam annotations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.