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

时间:2021-07-29 18:13:43

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

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

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

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@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\\\\/:\\*\\?\\\"<>\\|\\.]$为文件名称校验的正则表达式,复制进代码记得去掉自动生成的\。

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@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<ObjectError> 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<String, Object> getPublicBackValue(boolean flag, String message) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (flag) {
            map.put("result_code", 0);
        } else {
            map.put("result_code", 1);
        }
        map.put("result_reason", message);
        return map;
    }
}

@Valid校验异常捕捉

?
1
2
3
4
5
@Api(tags = {"参数管理"})
@Validated
@RestController
@RequestMapping("/module/param")
public class TbModuleParamController {}
?
1
2
3
4
5
6
7
8
9
10
public ResponseDTO getModuleParam(@PathVariable(name = "moduleId") @Valid @NotNull @Max(value = 13) @Min(value = 1) Integer moduleId) {
    QueryWrapper<TbModuleParam> paramQueryWrapper = new QueryWrapper<>();
    paramQueryWrapper.eq("module_id", moduleId).eq("state", 1);
    TbModuleParam moduleParam = moduleParamService.getOne(paramQueryWrapper);
    List<QueryParamVo> queryParamVoList = new ArrayList<>();
    if (moduleParam != null) {
        queryParamVoList = JSONArray.parseArray(moduleParam.getModuleJson(), QueryParamVo.class);
    }
    return ResponseDTO.defaultResponse(queryParamVoList);
}
?
1
2
3
4
5
@PostMapping(value = "/save", produces = WebServiceCommonConstant.PRODUCES_JSON)
public ResponseDTO<Boolean> addDict(@RequestBody @Validated LandInfoBasicVo saveVo) {
    boolean result = landInfoService.saveInfo(saveVo);
    return ResponseDTO.defaultResponse("保存成功");
}
?
1
2
3
@NotBlank(message = "土地名称不能为空")
@Size(max = 1)
private String landName;
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
@ControllerAdvice
public class ExceptionHandle {
    private static final Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);
    public static List<String> 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<String> 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<String> message = new ArrayList<>();
        Set<ConstraintViolation<?>> constraintViolations = ex.getConstraintViolations();
        if (!CollectionUtils.isEmpty(constraintViolations)) {
            constraintViolations.forEach(v -> message.add(v.getMessage()));
        }
        return ResponseDTO.customErrorResponse(-1, message.toString().replace("[","").replace("]",""));
    }
}

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

原文链接:https://allyixi.blog.csdn.net/article/details/100706810