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

网友投稿 661 2022-09-21


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

目录传参校验@Valid及对其的异常捕获@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&pQZiYlt;String, Object> 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();

fpQZiYor (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("]",""));

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:程序员常用的 API 接口管理工具有哪些?接口管理工具怎么用?
下一篇:HCIA-以太网帧结构-OSI模型
相关文章

 发表评论

暂时没有评论,来抢沙发吧~