您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。