SpringBoot2整合validation参数校验案例

一、相关依赖

        
        
            org.springframework.boot
            spring-boot-starter-validation
        

二、案例代码

入参bean

package com.ks.feecontrol.pojo.query;

import lombok.Data;

import javax.validation.constraints.*;
import java.io.Serializable;

/**
 * @author Mr.Zheng
 * @program: ks-platform
 * @description: 新增
 * @date 2020-06-08 10:33
 */
@Data
public class ColDeptAdd implements Serializable {
    /**
     * 科室id
     */
    @NotNull(message = "科室id不能为空")
    @NotBlank(message = "科室id不能为空")
    private String deptId;
    /**
     * 父科室id
     */
    @NotNull(message = "父科室id不能为空")
    @NotBlank(message = "父科室id不能为空")
    private String parentId;
    /**
     * 科室名称
     */
    @NotNull(message = "科室名称不能为空")
    @NotBlank(message = "科室名称不能为空")
    private String deptName;
    /**
     * 所属院区编码
     */
    @NotNull(message = "所属院区编码不能为空")
    @NotBlank(message = "所属院区编码不能为空")
    private String hospitalCode;
    /**
     * 所属院区名称
     */
    @NotNull(message = "所属院区名称不能为空")
    @NotBlank(message = "所属院区名称不能为空")
    private String hospitalName;
    /**
     * 1表示启用,0表示禁用
     */
    @NotNull(message = "科室状态不能为空")
    @DecimalMin(value = "0",message = "科室状态只能是0或者1")
    @DecimalMax(value = "1",message = "科室状态只能是0或者1")
    private Integer isValid;
}

 简单用法:

@RestController
@RequestMapping("/colDept")
public class ColDeptController implements ColDeptControllerApi {

    @Autowired
    private ColDeptService colDeptService;

    @Override
    @PostMapping("/addColDept")
    public ResponseResult addColDept(@Validated @RequestBody ColDeptAdd colDeptAdd, BindingResult bindingResult) {
        ResponseResult result = new ResponseResult<>();
        List errors = new ArrayList<>();
        if (bindingResult.hasErrors()) {
            List allErrors = bindingResult.getAllErrors();
            for (ObjectError error : allErrors) {
                errors.add(error.getDefaultMessage());
            }
            result.setRespCode(ResponseBaseEnum.API_ERROR_CODE_0020.getRespCode());
            result.setRespMsg(ResponseBaseEnum.API_ERROR_CODE_0020.getRespMsg());
            result.setSuccess(false);
            result.setData(errors);
            return result;
        }
        result.setRespCode(ResponseBaseEnum.API_ERROR_CODE_0000.getRespCode());
        result.setRespMsg(ResponseBaseEnum.API_ERROR_CODE_0000.getRespMsg());
        result.setSuccess(true);
        result.setData(colDeptService.addColDept(colDeptAdd));
        return result;
    }

} 
  

SpringBoot2整合validation参数校验案例_第1张图片

三、封装全局异常

拦截参数校验:

@ControllerAdvice
public class GlobalExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    ResponseResult methodArgumentNotValidException(MethodArgumentNotValidException e) {
        List fieldErrors=e.getBindingResult().getAllErrors();
        List errors = new ArrayList<>();
        for (ObjectError fieldError : fieldErrors) {
            errors.add(fieldError.getDefaultMessage());
        }
        ResponseResult result = new ResponseResult();
        result.failure(ResponseBaseEnum.API_ERROR_CODE_0020);
        result.setData(errors);
        return result;
    }
} 
  

修改简单用法案例案例

@RestController
@RequestMapping("/colDept")
public class ColDeptController implements ColDeptControllerApi {

    @Autowired
    private ColDeptService colDeptService;

    @Override
    @PostMapping("/addColDept")
    public ResponseResult addColDept(@Validated @RequestBody ColDeptAdd colDeptAdd) {
        ResponseResult result = new ResponseResult<>();
        result.setRespCode(ResponseBaseEnum.API_ERROR_CODE_0000.getRespCode());
        result.setRespMsg(ResponseBaseEnum.API_ERROR_CODE_0000.getRespMsg());
        result.setSuccess(true);
        result.setData(colDeptService.addColDept(colDeptAdd));
        return result;
    }

} 
  

 

你可能感兴趣的:(java,spring,servlet)