java---请求body实体校验和异常抛出处理(异常处理针对servert容器,webFlux 另一篇文章描述)
程序员文章站
2022-03-23 22:46:26
本文主要解决问题:1.实体参数校验通过框架层面解决,灵活使用javax.validation2.针对参数格式异常,统一抛出ParamException,框架层面拦截异常,解析成code和message返回给前端参数校验:1.controller入参前加上@Validated or @Valid2.参数如果表单嵌套 参数前加上@Validcontroller: @ApiOperation("用户注册") @PostMapping("/user-service/users/user"...
本文主要解决问题:
1.实体参数校验通过框架层面解决,灵活使用javax.validation
2.针对参数格式异常,统一抛出ParamException,框架层面拦截异常,解析成code和message返回给前端
参数校验:
1.controller入参前加上@Validated or @Valid
2.参数如果表单嵌套 参数前加上@Valid
controller:
@ApiOperation("用户注册")
@PostMapping("/user-service/users/user")
public CommonResult<Long> register(@Validated @RequestBody UserRequest userRequest) {
return CommonResult.ok(userService.register(userRequest));
}
request
@ApiModelProperty("用户名")
@NotEmpty(message = "姓名不能为空")
private String userName;
@ApiModelProperty("邮箱")
private String email;
@ApiModelProperty("手机")
@NotEmpty(message = "手机不能为空")
private String cellphone;
@NotEmpty(message = "角色不能为空")
@EnumValue(enumClass = Role.class, enumMethod = "isValidName")
@ApiModelProperty("角色")
private String role;
@ApiModelProperty("密码")
@NotEmpty(message = "密码不能为空")
private String password;
@ApiModelProperty("家庭信息")
@Valid
private Family family;
异常处理
BindException和MethodArgumentNotValidException 是spring验证框架抛出的异常,需要拦截解析处理
@Slf4j
@RestControllerAdvice
@Aspect
public class ExceptionAspect {
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(value = {GobalException.class})
@ResponseBody
public CommonResult<Void> resolvePlatformException(GobalException e) {
log.error("platform exception", e);
return CommonResult.error(e.getErrorCode(), e.getMessage());
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler({ParamException.class})
public CommonResult<Void> resolveParamException(ParamException e) {
log.error("param exception", e);
return CommonResult.error(e.getErrorCode(), e.getMessage());
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(BindException.class)
public CommonResult<Void> exceptionHandler(BindException e) {
String failMsg = Objects.requireNonNull(e.getBindingResult().getFieldError()).getField() + ":" + Objects
.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage();
log.error("bind exception", e);
return CommonResult.error(ErrorCode.PARAMETER_ERROR.getCode(), failMsg);
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException.class)
public CommonResult<Void> exceptionHandler(MethodArgumentNotValidException e) {
String failMsg = Objects.requireNonNull(e.getBindingResult().getFieldError()).getField() + ":" + Objects
.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage();
log.error("bind exception", e);
return CommonResult.error(ErrorCode.PARAMETER_ERROR.getCode(), failMsg);
}
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public CommonResult<Void> resolveException(Exception e) {
log.error(" exception", e);
return CommonResult.error(ErrorCode.COMMON_SERVER_ERROR.getCode(), ErrorCode.COMMON_SERVER_ERROR.getMessage());
}
}
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
本文地址:https://blog.csdn.net/u014469254/article/details/109621594
上一篇: 什么是死锁
下一篇: 基于Python正确读取资源文件