这篇文章将为大家详细讲解有关springboot如何传参校验@Valid及对其的异常捕获方式,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
springboot参数经常需要进行校验,比如创建文件,文件名就需要进行一定的校验。
本文以创建文件夹为例进行参数校验:controller:
首先就是在需要校验的参数类前面添加注释@Valid
@ApiOperation(value = "创建目录", notes = "在某目录下创建新文件夹") @ApiResponses({ @ApiResponse(code = 500, response = RestCodeMsg.class, message = "错误") }) @PostMapping(value = "api/scene/createdir") public ResponseEntity<Map> 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<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; } }
@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<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); }
@PostMapping(value = "/save", produces = WebServiceCommonConstant.PRODUCES_JSON) public ResponseDTO<Boolean> 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<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("]","")); } }
关于springboot如何传参校验@Valid及对其的异常捕获方式就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。