@RequestParam 注解接收的参数

value:参数名称,与 name 一样
name:参数名称,与 value 一样
required:是否必须,默认为 true
defaultValue:默认值,默认为 null

测试

下面通过控制 requireddefaultValue 参数,测试 @RequestParam 在接以空参数未传参数时的表现。

测试一

定义一个 GET 请求接口,param 参数为必传(默认就是必传的):

  1. @GetMapping("/test01")
  2. public String test01(@RequestParam(name = "param", required = true) String param) {
  3. return "param = " + param;
  4. }

不传 param 参数时,则返回 404

  1. GET http://localhost:8080/test01
  2. {
  3. "timestamp": "2021-10-12T02:02:35.071+00:00",
  4. "status": 400,
  5. "error": "Bad Request",
  6. "message": "",
  7. "path": "/test01"
  8. }

传的 param 参数为空串时,接收到 param 为空串:

  1. GET http://localhost:8080/test01?param=
  2. param =

测试二

定义一个 GET 请求接口,param 参数为非必传:

  1. @GetMapping("/test02")
  2. public String test02(@RequestParam(name = "param", required = false) String param) {
  3. return "param = " + param;
  4. }

不传 param 参数时,接收到 paramnull

  1. GET http://localhost:8080/test02
  2. param = null

param 传了空串时,接收到 param 为空串:

  1. GET http://localhost:8080/test02?param=
  2. param =

测试三

定义一个 GET 请求接口,param 参数为非必传,并指定 defaultValue 默认值:

  1. @GetMapping("/test03")
  2. public String test03(@RequestParam(name = "param", required = false, defaultValue = "defaultValue")
  3. String param) {
  4. return "param = " + param;
  5. }

不传 param 参数时,则 param 为默认值:

  1. GET http://localhost:8080/test03
  2. param = defaultValue

如果 param 传了空串,param 为默认值

  1. GET http://localhost:8080/test03?param=
  2. param = defaultValue

测试四

定义一个 GET 请求接口,param 参数为必传,并指定 defaultValue 默认值:

  1. @GetMapping("/test04")
  2. public String test04(@RequestParam(name = "param", required = true, defaultValue = "defaultValue")
  3. String param) {
  4. return "param = " + param;
  5. }

不传 param 参数时,则 param 为默认值:

  1. GET http://localhost:8080/test04
  2. param = defaultValue

param 传了空串时,param 为默认值:

  1. GET http://localhost:8080/test04?param=
  2. param = defaultValue

测试总结

只有 requiredtrue 时没有传递参数会出现 404 的情况,这一点比较诡异。

优雅地校验参数

引入相关依赖

  1. <dependency>
  2. <groupId>jakarta.validation</groupId>
  3. <artifactId>jakarta.validation-api</artifactId>
  4. <version>2.0.2</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.hibernate</groupId>
  8. <artifactId>hibernate-validator</artifactId>
  9. <version>5.4.1.Final</version>
  10. </dependency>

在Spring容器注入MethodValidationPostProcessor对象

  1. import org.springframework.context.annotation.Configuration;
  2. import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
  3. @Configuration
  4. public class ValidationConfigure {
  5. public MethodValidationPostProcessor methodValidationPostProcessor() {
  6. return new MethodValidationPostProcessor();
  7. }
  8. }

新建全局异常拦截器

  1. @ControllerAdvice
  2. public class GlobalExceptionHandler {
  3. @ExceptionHandler(ConstraintViolationException.class)
  4. @ResponseBody
  5. @ResponseStatus(HttpStatus.BAD_REQUEST)
  6. public String handleValidationException(ConstraintViolationException e) {
  7. return e.getConstraintViolations().stream()
  8. .map(ConstraintViolation::getMessage)
  9. .collect(Collectors.joining("; "));
  10. }
  11. }

测试

在控制器类上方增加 @org.springframework.validation.annotation.Validated 注解,新建一个 GET 请求接口,在 param 参数中使用校验注解 @org.hibernate.validator.constraints.NotBlank

  1. @RestController
  2. @Validated
  3. public class TestController {
  4. @GetMapping("/test05")
  5. public String test05(@NotBlank(message = "param is not blank")
  6. @RequestParam(value = "param")
  7. String param) {
  8. return "param = " + param;
  9. }
  10. }

如果 param 传了空串,返回参数校验异常信息:

  1. GET http://localhost:8080/test05?param=
  2. param is not blank