Discussion on SpringBoot's handling of the parameter annotation in url

  • 2020-12-18 01:48:46
  • OfStack

1. How to deal with the parameters in url

@PathVaribale gets the data in url

@RequestParam gets the value of the request parameter

Combined annotation @GetMapping, short for @RequestMapping (method = RequestMethod.GET)

(1)PathVaribale obtained the data in url

Look at an example, if we need to get Url = localhost: 8080 / hello/id id value, in the implementation code is as follows:


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

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

id:81

Again, if we need more than one parameter to get at url, we can do it as shown in the code below.


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

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 getting the parameters in URL via the @PathVariable annotation is that we know what the url format looks like.

Only if we know the format of url can we get the parameter values for the corresponding location in the same format on the specified method.

1 In general, the format of url is: localhost:8080/hello? id=98, in which case how do I get its id value, which I need to do with @RequestParam

2.@RequestParam gets the value of the request parameter

Such as:


@RestController
public 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, you can see the following results:

id: 1000

When we enter the address: localhost:8080/hello? id, or do not enter the specific value of id, the result is returned as null. The specific test results are as follows:

id: null

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

whitelable Error Page error

The @ES105en annotation provides us with a solution that allows users to use default values when they do not enter id, as follows:


@RestController
public class HelloController {
 @RequestMapping(value="/hello",method= RequestMethod.GET)
 //required=false  said url It can be worn out id Parameter, the default parameter is used at this point 
 public String sayHello(@RequestParam(value="id",required = false,defaultValue = "1") Integer id){
 return "id:"+id;
 }
}

If there are multiple parameters in url, that is, similar to localhost:8080/hello? id=98 & & name=helloworld So url, you can do the same thing. The specific code is as follows:


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

Test results in the browser are as follows: localhost: 8080/hello? id=1000 & name=helloworld address displays the following:

id: 1000 name: helloworld

3. @GetMapping combined annotation

@GetMapping is a combinational annotation, short for @RequestMapping (method = RequestMethod.GET). This annotation maps HTTP Get to a specific processing method.

You can use @GetMapping (value = "/hello") instead of @RequestMapping (value= "/hello",method= RequestMethod.GET). That allows us to simplify the code.


@RestController
public class HelloController {
//@RequestMapping(value="/hello",method= RequestMethod.GET)
@GetMapping(value = "/hello")
//required=false  said url It can be worn out id Parameter, the default parameter is used at this point 
public String sayHello(@RequestParam(value="id",required = false,defaultValue = "1") Integer id){
 return "id:"+id;
 }
 }

4.PostMapping combination notes:

Methods with GetMapping


Related articles: