validator参数校验

发布于:2024-06-14 ⋅ 阅读:(68) ⋅ 点赞:(0)

1. validator 校验机制

开发中经常需要校验前端传递的参数: 代码比较臃肿

@GetMapping("/test")
public String test0( Integer id){
    if(id==null){
        return "error";
    }
    return "success";
}

如何解决上述问题呢

JSR303 是一套JavaBean参数校验的标准,它定义了很多常用的校验注解,我们可以直接将这些注解加在我们JavaBean的属性上面(面向注解编程的时代),就可以在需要校验的时候进行校验了,在SpringBoot中已经包含在starter-web中,在其他项目中可以引用依赖,并自行调整版本:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

2. 校验方式

  • 直接在controller的方法中的参数前加上@NotNull,@Min等注解。

这种方式必须在controller上加@Validated注解.
如果校验不通过会产生org.springframework.web.bind.MethodArgumentNotValidException异常

  • 在实体类中需要进行校验的参数上方加@NotNull,@Min等注解。

这种方式必须在controller方法中需要校验的参数前加上@Validated注解(也可以用@Valid)
如果校验不通过会产生javax.validation.ConstraintViolationException;异常

TestValController

import com.gzdemo.springbootdemo.pojos.UserDto;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/user")
@Validated
public class TestValController {

    @GetMapping("/test")
    public String test0(Integer id){
        if(id==null){
            throw  new RuntimeException("参数异常");
        }
        return "success";
    }

    // 方式一
    @GetMapping("/test2")
    public String test2(@NotNull(message = "id不能为空") Integer id,
                        @Min(value = 0,message = "age必须>=0") @Max(value = 100,message = "age必须<=100") Integer age){
        return "success";
    }

    // 方式二
    @PostMapping("/test3")
    public String test3(@Validated @RequestBody UserDto userDto){
        return "success";
    }

UserDto

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.*;

@Data
public class UserDto {

    @ApiModelProperty(value="id",required = true)
    @NotNull(message = "id不能为Null")
    private Integer id;

    @ApiModelProperty(value="姓名",required = true)
    @NotBlank(message = "name不能为空")
    private String name;

    @ApiModelProperty(value="手机号",required = true)
    @Pattern(regexp = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$",message = "手机号格式不正确")
    private String phone;

    @ApiModelProperty(value="年龄",required = true)
    @Max(value = 100,message = "age不能超过100")
    @Min(value = 0,message = "age不能小于0")
    private Integer age;

    @ApiModelProperty(value="邮箱",required = true)
    @Pattern(regexp = "^[A-Za-z0-9\\u4e00-\\u9fa5]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$",message = "邮箱格式不正确")
    private String email;
}

其他注解请参考文章:

https://blog.csdn.net/tianzhonghaoqing/article/details/116978091

3. 定义全局异常处理器

可以通过捕获参数校验产生的异常,直接返回前端校验信息。

package com.gzdemo.springbootdemo.advice;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.validation.ObjectError;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        String msg = e.getBindingResult().getAllErrors()
                .stream().map(ObjectError::getDefaultMessage)
                .collect(Collectors.joining("|"));
        log.error("请求参数校验异常 -> MethodArgumentNotValidException, {}", msg);
        log.debug("", e);
        return RespResult(400, 400, msg);
    }

    @ExceptionHandler(ConstraintViolationException.class)
    public Object handViolationException(ConstraintViolationException e) {
        String msg = e.getConstraintViolations()
                .stream().map(ConstraintViolation::getMessage)
                .distinct().collect(Collectors.joining("|"));
        log.error("请求参数异常 -> ConstraintViolationException, {}", e.getMessage());

        return RespResult( HttpStatus.OK.value(), HttpStatus.BAD_REQUEST.value(),msg);
    }

    @ExceptionHandler(BindException.class)
    public Object handleBindException(BindException e) {
        log.error("请求参数绑定异常 ->BindException, {}", e.getMessage());
        log.debug("", e);
        return RespResult(400, 400, "请求参数格式错误");
    }
    
    public Object RespResult(int status, int code, String msg){
        Map resultMap = new HashMap<String,Object>();
        resultMap.put("status",status);
        resultMap.put("code",code);
        resultMap.put("msg",msg);
        return resultMap;
    }
}

4. 测试

  • 测试第一种方式参数校验

在这里插入图片描述

  • 测试RequestBody参数校验

在这里插入图片描述

5. 分组校验