SpringBoot processes parameter annotation in url, springbooturl

Source: Internet
Author: User

SpringBoot processes parameter annotation in url, springbooturl

1. describes how to process annotation of parameters in a url.

@ PathVaribale: Get the data in the url

@ RequestParam: Get the value of the Request Parameter

@ GetMapping combination annotation, abbreviated as @ RequestMapping (method = RequestMethod. GET)

(1) PathVaribale obtains the data in the url.

Let's look at an example. If we need to obtain the id value in Url = localhost: 8080/hello/id, the implementation code is as follows:

1 @RestController2 public class HelloController {3     @RequestMapping(value="/hello/{id}/{name}",method= RequestMethod.GET)4     public String sayHello(@PathVariable("id") Integer id,@PathVariable("name") String name){5         return "id:"+id+" name:"+name;6     }7 }

Enter the address: localhost: 8080/hello/100/helloworld in the browser and print it out on the html page:

Id: 81

 

Similarly, if we need to obtain multiple parameters in the url, we can do it as shown in the following code.

1 @RestController2 public class HelloController {3     @RequestMapping(value="/hello/{id}/{name}",method= RequestMethod.GET)4     public String sayHello(@PathVariable("id") Integer id,@PathVariable("name") String name){5         return "id:"+id+" name:"+name;6     }7 }

Enter the address: localhost: 8080/hello/100/helloworld in the browser and print it out on the html page:

Id: 100 name: helloworld

Above, the prerequisite for obtaining URL parameters through @ PathVariable annotation is that we know what the url format is like.
Only by knowing the url format can we get the parameter values at the corresponding position through the same format in the specified method.
Generally, the url format is: localhost: 8080/hello? Id = 98. In this case, how can I obtain the id value? This requires @ RequestParam.

  

2. @ RequestParam get the value of the Request Parameter

For example:

@RestControllerpublic class HelloController {    @RequestMapping(value="/hello",method= RequestMethod.GET)    public String sayHello(@RequestParam("id") Integer id){        return "id:"+id;    }}

Enter the address: localhost: 8080/hello? in the browser? Id = 1000. the following result is displayed:

Id: 1000

In the browser, enter the address: localhost: 8080/hello? Id, that is, no specific value of id is input. The returned result is null. The test results are as follows:

Id: null

However, when we enter the address localhost: 8080/hello in the browser, that is, if the id parameter is not entered, the following error will be reported:

Whitelable Error Page Error

 

@ RequestParam annotation provides us with this solution, that is, when users are not allowed to enter the id, use the default value. The specific code is as follows:

1 @ RestController2 public class HelloController {3 @ RequestMapping (value = "/hello", method = RequestMethod. GET) 4 // required = false indicates that the id parameter can be left blank in the url. The default parameter 5 public String sayHello (@ RequestParam (value = "id", required = false, defaultValue = "1") Integer id) {6 return "id:" + id; 7} 8}

If there are multiple parameters in the url, that is, similar to localhost: 8080/hello? URLs such as id = 98 & name = helloworld can also be processed in this way. The Code is as follows:

1 @RestController2 public class HelloController {3     @RequestMapping(value="/hello",method= RequestMethod.GET)4     public String sayHello(@RequestParam("id") Integer id,@RequestParam("name") String name){5         return "id:"+id+ " name:"+name;6     }7 }

The test result in the browser is as follows: localhost: 8080/hello? Id = 1000 & name = helloworld address, the following content is displayed:
Id: 1000 name: helloworld

 

3. @ GetMapping combination Annotation

@ GetMapping is a composite annotation, abbreviated as @ RequestMapping (method = RequestMethod. GET. This annotation maps HTTP Get to a specific processing method.
You can use @ GetMapping (value = "/hello") to replace @ RequestMapping (value = "/hello", method = RequestMethod. GET ). This allows us to streamline the code.

1 @ RestController2 public class HelloController {3 // @ RequestMapping (value = "/hello", method = RequestMethod. GET) 4 @ GetMapping (value = "/hello") 5 // required = false indicates that the id parameter can be left blank in the url, in this case, use the default parameter 6 public String sayHello (@ RequestParam (value = "id", required = false, defaultValue = "1") Integer id) {7 return "id: "+ id; 8} 9}

4. PostMapping combination annotation:

The method is the same as GetMapping.

 

Reference: http://blog.csdn.net/u010412719/article/details/69788227

Related Article

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.