1. Introduction to several annotations on how to handle parameters in url
@PathVaribale Get data in url
@RequestParam Get the value of the request parameter
@GetMapping Combination Annotation is the abbreviation of @RequestMapping(method = RequestMethod.GET)
(1)PathVaribale Gets the data in the url
Looking at an example, if we need to get the id value in Url=localhost:8080/hello/id, the implementation code is as follows:
@RestControllerpublic class HelloController { @RequestMapping(value="/hello/{id}/{name}",method= RequestMethod.GET) public String saysHello(@PathVariable("id") Integer id,@PathVariable("name") String name){ return "id:"+id+" name:"+name; }}Enter the address in the browser: localhost: 8080/hello/100/helloworld and then print it on the html page:
id:81
Similarly, if we need to have multiple parameters in the url to obtain, then we can do it as shown in the following code.
@RestControllerpublic class HelloController { @RequestMapping(value="/hello/{id}/{name}",method= RequestMethod.GET) public String saysHello(@PathVariable("id") Integer id,@PathVariable("name") String name){ return "id:"+id+" name:"+name; }}Enter the address in the browser: localhost: 8080/hello/100/helloworld and then print it on the html page:
id: 100 name: helloworld
The prerequisite for obtaining parameters in a URL through the @PathVariable annotation is that we know the format of the url.
Only by knowing the format of the url can we obtain the parameter values at the corresponding position through the same format on the specified method.
Generally speaking, the format of the url is: localhost:8080/hello?id=98. How to get its id value in this case? This requires the help of @RequestParam.
2.@RequestParam Get the value of the request parameter
For example:
@RestControllerpublic class HelloController { @RequestMapping(value="/hello",method= RequestMethod.GET) public String saysHello(@RequestParam("id") Integer id){ return "id:"+id; }}Enter the address in the browser: localhost:8080/hello?id=1000, and you can see the following results:
id: 1000
When we enter the address in the browser: localhost:8080/hello?id, that is, we do not enter the specific value of id, the result returned is null. The specific test results are as follows:
id: null
However, when we enter the address in the browser: localhost:8080/hello, that is, without entering the id parameter, the following error will be reported:
whitelable Error Page Error
The @RequestParam annotation provides us with this solution, that is, when the user does not enter the id, the default value is used. The specific code is as follows:
@RestControllerpublic class HelloController { @RequestMapping(value="/hello",method= RequestMethod.GET) //required=false means that the id parameter can be entered in the url, and the default parameter will be used at this time. Public String saysHello(@RequestParam(value="id",required = false,defaultValue = "1") Integer id){ return "id:"+id; }}If there are multiple parameters in the url, that is, a url similar to localhost:8080/hello?id=98&&name=helloworld, it can also be handled in this way. The specific code is as follows:
@RestControllerpublic class HelloController { @RequestMapping(value="/hello",method= RequestMethod.GET) public String saysHello(@RequestParam("id") Integer id,@RequestParam("name") String name){ return "id:"+id+ " name:"+name; }}The test results in the browser are as follows: localhost:8080/hello? id=1000&name=helloworld address, the following content will be displayed:
id: 1000 name: helloworld
3.@GetMapping Combination Annotation
@GetMapping is a combination annotation, which is the abbreviation of @RequestMapping(method = RequestMethod.GET). This annotation maps HTTP Get to a specific processing method.
That is, you can use @GetMapping(value = "/hello") instead of @RequestMapping(value = "/hello", method= RequestMethod.GET). That is, let us streamline the code.
@RestControllerpublic class HelloController {//@RequestMapping(value="/hello",method= RequestMethod.GET)@GetMapping(value = "/hello")//required=false It means that the id parameter can be entered in the url, and the default parameter will be used at this time. Public String saysHello(@RequestParam(value="id",required = false,defaultValue = "1") Integer id){ return "id:"+id; } }4.PostMapping combination annotations:
The same method as GetMapping
The above annotation on the parameters in SpringBoot processing of url is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.