springboot 傳參校驗@Valid及對其的異常捕獲方式
傳參校驗@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; } }
@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<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("]","")); } }
以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。
推薦閱讀:
- Spring 使用Validation 驗證框架的問題詳解
- SpringBoot中controller深層詳細講解
- SpringBoot中如何統一接口返回與全局異常處理詳解
- @validated註解異常返回JSON值方式
- Spring Boot使用JSR-380進行校驗的示例