文章目录

  • 一、SpringBoot中的参数传递注解
  • 1、@PathVariable
  • 2、@RequestParam
  • 3、@RequestBody
  • 4、不用注解或者@ModelAttribute
  • 二、参数类型校验失败后的三种异常


一、SpringBoot中的参数传递注解

先看看非json下参数的接收和传递:

1、@PathVariable

@PathVariable注解用于从url来获取参数值。URL 中的 {xxx} 占位符可以通过 @PathVariable(“xxx“) 绑定到操作方法的入参中

@RestController
@RequestMapping("/test")
public class TestController {

    @GetMapping("/testPathVariable/{id}/{name}")
    public Object testPathVariable(@PathVariable(value = "id") Integer id, @PathVariable(value = "name") String username){
		return null;
	}
}

这里不管是get还是post都一样,在路径传参:

springboot传参给xml 文件 springboot传参的注解_spring boot

2、@RequestParam

@PathValiable 是从 url 模板中获取参数值, 即这种风格的 url:http://localhost:8088/test/testPathVariable/{id}

@RequestParam 是从 request 表单里面获取参数值,即这种风格的 url: http://localhost:8088/test/testRequestparam?id=1

GET下的@RequestParam

@RestController
@RequestMapping("/test")
public class TestController {

    @GetMapping("/testRequestParam")
    public Object testRequestParam(@RequestParam Integer id){
		return null;
	}
}

调用:

springboot传参给xml 文件 springboot传参的注解_java_02

当然不是只能int、String,Map集合、数组都行:参考这里

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello(@RequestParam Map<String, Object> map){
 
        return "图书: " + map.get("name") +  " 的作者为: " + map.get("author");
    }
}

springboot传参给xml 文件 springboot传参的注解_实体类_03

@RequestParam注解可以设置value属性和required属性:

  • value设置前端传参时的参数名。@RequestParam(value = “name”) String username,这时前端传参名必须是name
  • required属性设置这个参数是否为必传,false时,默认给对应参数赋值null。注意如果参数是int类型,赋值null会报错

POST下的@RequestParam

假设此时需要从前端接受username和password:

@PostMapping("/testRequestParam")
    public Object testRequestParam(@RequestParam String username, @RequestParam String password){
        return null;
    }

调用:

springboot传参给xml 文件 springboot传参的注解_spring boot_04


但其实当要接收的参数较多的时候,这样一个个参数去写不合理,应该封装到一个dto类中,使用别的注解接参。接下来看json传参,用实体类(Dto类)接参的注解:


3、@RequestBody

@RequestBody注解用于POST请求上,接收json实体参数.

@PostMapping("/testRequestBody")
    public Object testRequestBody(@RequestBody AddUserDto dto) {
        
        return null;
    }

调用:

springboot传参给xml 文件 springboot传参的注解_实体类_05

4、不用注解或者@ModelAttribute

刚才说到:controller层用实体类接收传参的时候,POST请求用@RequestBody注解,而GET请求,用dto实体类去接收前端传参时,是不加注解或者用@ModelAttribute

写法1:

@GetMapping("/testGetDto")
    public Object testGetDto( UserInfoDto dto) {
        
        return null;
    }

//接收到的username和password是实体中的属性,此时SpringBoot会帮我们自动填充到实体中

写法2:

@GetMapping("/testGetDto")
    public Object testGetDto(@ModelAttribute UserInfoDto dto) {
        
        return null;
    }

调用:

springboot传参给xml 文件 springboot传参的注解_spring boot_06

总结:

  • controller层使用基本数据类型来接参,用@PathVariable或者@RequestParam
  • controller层使用dto实体类接参,POST用@RequestBody,GET不用注解或者用@ModelAttribute

二、参数类型校验失败后的三种异常

日常开发中,常常在dto类的属性中使用注解进行参数合法性校验,如@NotNull、@Min,然后使用@Valid或者@Validated开启校验,当校验不通过时,抛出异常,由全局异常处理器去捕捉拦截处理。而不同的情况下,抛出的是不同类型的异常:

  • MethodArgumentNotValidException异常:@RequestBody(POST)接参验证失败后抛出
  • ConstraintViolationException异常: @RequestParam接参验证失败后抛出
  • BindException异常:GET中使用@ModelAttribute或者空注解接参,验证失败后抛出