springboot 传参校验@Valid及对其的异常捕获方式

传参校验@Valid及对其的异常捕获

springboot参数经常需要进行校验,比如创建文件,文件名就需要进行一定的校验。

本文以创建文件夹为例进行参数校验:controller:

首先就是在需要校验的参数类前面添加注释@Valid

@ApiOperation(value = "创建目录", notes = "在某目录下创建新文件夹")
    @ApiResponses({
            @ApiResponse(code = 500, response = RestCodeMsg.class, message = "错误")
    })
    @PostMapping(value = "api/scene/createdir")
    public ResponseEntity createNewOrEditFile(@RequestBody @Valid ixviewVo ixveVo) {
     ....
     //校验与内容无关
    }

其次对参数类进行校验设置:

@Data
@ApiModel
@Getter
@Setter
@NoArgsConstructor
public class ixviewVo {
    @ApiModelProperty("是否文件夹")
    private boolean dir;
    @NotBlank(message="目录名称不能为空")
    @Pattern(regexp="[^\\s\\\\/:\\*\\?\\\"<>\\|\\.]*[^\\s\\\\/:\\*\\?\\\"<>\\|\\.]$",message="目录名称不符合标准")
    @ApiModelProperty("目录名称")
    private String dirname;
    @ApiModelProperty("上级目录ID")
    private Long parentId;
}

其中[^\\s\\\\/:\\*\\?\\\"<>\\|\\.]*[^\\s\\\\/:\\*\\?\\\"<>\\|\\.]$为文件名称校验的正则表达式,复制进代码记得去掉自动生成的\。

到此,对参数校验的全部设置完成。当参数不符合校验则会抛出异常,接下来就是对抛出的异常进行捕获:

@RestControllerAdvice
public class BadRequestExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(BadRequestExceptionHandler.class);
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity validationBodyException(MethodArgumentNotValidException exception){
        BindingResult result = exception.getBindingResult();
        if (result.hasErrors()) {
            List errors = result.getAllErrors();
            errors.forEach(p ->{
                FieldError fieldError = (FieldError) p;
                logger.error("Data check failure : object{"+fieldError.getObjectName()+"},field{"+fieldError.getField()+
                        "},errorMessage{"+fieldError.getDefaultMessage()+"}");
            });
        }
        return ResponseEntity.ok(getPublicBackValue(false, "目录名称不符合标准"));
    }
    public Map getPublicBackValue(boolean flag, String message) {
        Map map = new HashMap();
        if (flag) {
            map.put("result_code", 0);
        } else {
            map.put("result_code", 1);
        }
        map.put("result_reason", message);
        return map;
    }
}

@Valid校验异常捕捉

@Api(tags = {"参数管理"})
@Validated
@RestController
@RequestMapping("/module/param")
public class TbModuleParamController {}
    public ResponseDTO getModuleParam(@PathVariable(name = "moduleId") @Valid @NotNull @Max(value = 13) @Min(value = 1) Integer moduleId) {
        QueryWrapper paramQueryWrapper = new QueryWrapper<>();
        paramQueryWrapper.eq("module_id", moduleId).eq("state", 1);
        TbModuleParam moduleParam = moduleParamService.getOne(paramQueryWrapper);
        List queryParamVoList = new ArrayList<>();
        if (moduleParam != null) {
            queryParamVoList = JSONArray.parseArray(moduleParam.getModuleJson(), QueryParamVo.class);
        }
        return ResponseDTO.defaultResponse(queryParamVoList);
    }
    @PostMapping(value = "/save", produces = WebServiceCommonConstant.PRODUCES_JSON)
    public ResponseDTO addDict(@RequestBody @Validated LandInfoBasicVo saveVo) {
        boolean result = landInfoService.saveInfo(saveVo);
        return ResponseDTO.defaultResponse("保存成功");
    }
    @NotBlank(message = "土地名称不能为空")
    @Size(max = 1)
    private String landName;
@ControllerAdvice
public class ExceptionHandle { 
    private static final Logger logger = LoggerFactory.getLogger(ExceptionHandle.class); 
    public static List msgList = new ArrayList<>();
 
    /**
     * 异常处理
     *
     * @param e 异常信息
     * @return 返回类是我自定义的接口返回类,参数是返回码和返回结果,异常的返回结果为空字符串
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResponseDTO handle(Exception e) {
        //自定义异常返回对应编码
        if (e instanceof PermissionException) {
            PermissionException ex = (PermissionException) e;
            return ResponseDTO.customErrorResponse(ex.getCode(), ex.getMessage());
        }
        //其他异常报对应的信息
        else {
            logger.info("[系统异常]{}", e.getMessage(), e);
            msgList.clear();
            msgList.add(e.toString());
            StackTraceElement[] stackTrace = e.getStackTrace();
            for (StackTraceElement element : stackTrace) {
                msgList.add(element.getClassName() + ":" + element.getMethodName() + "," + element.getLineNumber());
            }
            return ResponseDTO.customErrorResponse(-1, "系统内部错误");
        } 
    }
 
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    @ResponseBody
    public ResponseDTO handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
        List message = new ArrayList<>();
        if (ex.getBindingResult() != null) {
            for (FieldError item : ex.getBindingResult().getFieldErrors()) {
                String itemMessage = item.getDefaultMessage();
                message.add(itemMessage);
            }
        }
        return ResponseDTO.customErrorResponse(-1, message.toString().replace("[","").replace("]",""));
    } 
 
    @ExceptionHandler(value = ConstraintViolationException.class)
    @ResponseBody
    public ResponseDTO handleConstraintViolationException(ConstraintViolationException ex) {
        List message = new ArrayList<>();
        Set> constraintViolations = ex.getConstraintViolations();
        if (!CollectionUtils.isEmpty(constraintViolations)) {
            constraintViolations.forEach(v -> message.add(v.getMessage()));
        }
        return ResponseDTO.customErrorResponse(-1, message.toString().replace("[","").replace("]",""));
    }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

你可能感兴趣的:(springboot 传参校验@Valid及对其的异常捕获方式)