原创:全局异常捕获BindException、MethodArgumentNotValidException和ConstraintViolationException @Validated@Valid 2021-04-28 18:14:01

一般entity的参数校验,异常就这如下三种:

BindException(@Validated @Valid 仅对于 表单提交有效 ,对于以json格式提交将会失效 ):

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(BindException.class)
    public ResultVO bindExceptionHandler(BindException e) {
        List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
        List<String> msgList = fieldErrors.stream()
                .map(o -> o.getDefaultMessage())
                .collect(Collectors.toList());
        String messages = StringUtils.join(msgList.toArray(), ";");
        log.error("Validation格式校验异常:-------------->{}",messages);
        return ResultVO.builder().errorcode(HttpStatus.BAD_REQUEST.value()).errormessage(messages).build();

 MethodArgumentNotValidException(@Validated @Valid 前端提交的方式为json格式有效,出现异常时会被该异常类处理):

* @Validated 校验错误异常处理 @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(value = MethodArgumentNotValidException.class) public ResultVO handler(MethodArgumentNotValidException e) throws IOException { BindingResult bindingResult = e.getBindingResult(); ObjectError objectError = bindingResult.getAllErrors().stream().findFirst().get(); String messages = objectError.getDefaultMessage(); log.error("MethodArgumentNotValidException异常:-------------->{}", messages); return ResultVO.builder().errorcode(400).errormessage(messages).build();

 ConstraintViolationException(@NotBlank @NotNull @NotEmpty):

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = ConstraintViolationException.class)
public ResultVO handler(ConstraintViolationException e) throws IOException {
    List<String> msgList = new ArrayList<>();
    for (ConstraintViolation<?> constraintViolation : e.getConstraintViolations()) {
        msgList.add(constraintViolation.getMessage());
    String messages = StringUtils.join(msgList.toArray(), ";");
    log.error("entity格式校验异常:-------------->{}",messages);
    return ResultVO.builder().errorcode(400).errormessage(messages).build();

https://www.liuyanzhao.com/8000.html

https://blog.csdn.net/u014082714/article/details/107160281/

  错误: 代理抛出异常错误: java.rmi.server.ExportException: Port already in use: 1099; nested exception is: java.net.BindException: Address already in use: JVM_Bind   这里说的是1099端口被其它进程占用了. 二.解决办法   找出占用1099端口的进程,进入windows命令,查看什么进程占用了1099端口   使用命令: MethodArgumentNotValidException 异常同样也有两种处理方式继承org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler 类,重写 handleMethodArgumentNotValid() 方法通过 @ExceptionHandler 注解指定要处理的异常,并在处理方法中处理不同的参数校验方式会产生不同的异常。 在2.3.0版本之前spring-boot-starter-web是集成了validation检验的 但是在2.3.0开始就去掉了该依赖,所以需要自己添加该依赖。这里要注意,如果是集合对象,同样需要添加@Valid注解,不然不生效的。参数如果是非对象格式,需要在controller类上面添加@参数如果是对象的话,属性的前面的需要添加 @Valid注解。 public class WordsRequest implements Serializable { private static final long serialVersionUID = 4048730427527061626L; @ApiModelProperty("昵称") private String nickname; @ApiModelProp... @NotNull:验证对象是否不为null, 无法查检长度为0的字符串 @NotBlank:检查约束 (字符串) 是不是Null还有被Trim的长度是否大于0,只对字符串,且会去掉前后空格 @NotEmpty:检查(集合)约束元素是否为NULL或者是EMPTY @Size(min=,max=):验证对象(Array,Collection,Map,String)长度是否在 @PostMapping("/update") @RequiresPermissions("user:update") public R update(@RequestBody @Valid UserEntity user) { userService.update(user); return R.ok(); @NotNull(message = "手机号不能为空") @Pattern(regexp = "^[1][3,4,5,6,7,8,9][0-9]{9}$", message = "手机号格式有误") String phone; 异常处理的类 @ControllerAdvice @ResponseBody public class GlobleExceptionHandle 数据校验前言正文单个对象一组对象结束 大多数项目中都需要后台对传过来的对象进行校验,比如手机号的位数,特殊字段不能为空等等。之前我们可能都是使用if…else…,今天我们了解一下validated 想要使用validated需要引入Jar包,有两个方式,选择一种即可: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-bo 年关将至,对于大部分程序员来说,马上就可以闲下来一段时间了,然而在这个闲暇的时间里,唯有争论哪门语言更好可以消磨时光,估计最近会有很多关于java与.net的博文出现,我表示要作为一个吃瓜群众,静静的看着大佬们发表心情。 以上的废话说的够多了,这里就不再废话了,还是切入正题吧。 在项目开发中,对于系统和代码的稳定性和容错性都是有对应的要求。实际开发项目中的代码与样例代码的区别,更多的是在代码的运行的稳定性、容错性、扩展性的比较。因为对于实现一个功能来说,实现功能的核心代码是一样的,可能只是在写法上优化而已,但是在实现某一个操作上使用的类来说,这一点是绝大多数时候是一样的。这样看来,我们在实际开 异常共三种 BindException:表单提交有效,对于以json格式提交将会失效 MethodArgumentNotValidException:前段以json格式有效 ConstraintViolationException :参数上加@RequestParam或参数加@NotBlank @NotNull等