zoukankan      html  css  js  c++  java
  • springBoot获取@NotBlank,@NotNull注解的message信息

    概述

      springBoot后台验证接收的参数是否不合法时,会抛出一个BlndException异常,获取message的自定义信息并返回

    验证

    UserEntity类

    @Data
    @Entity
    @Table(name = "t_user")
    @ApiModel
    public class UserEntity
    {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private long id;
    
        @NotBlank(message = "username不能为空")
        @Column(name = "name")
        private String username;
    
        @NotBlank(message = "password不能为空")
        private String password;
    }

     统一异常处理

    @ResponseBody
        @ExceptionHandler(BindException.class)
        public ResponseJsonResult exceptionHandler(BindException e)
        {
            e.printStackTrace();
            ResponseJsonResult responseJsonResult = new ResponseJsonResult();
            responseJsonResult.setState(Constant.ERROR);
            responseJsonResult.setErrorCode(ExceptionCode.IV00007);
            responseJsonResult.setMessage(e.getBindingResult().getFieldError().getDefaultMessage());
            return responseJsonResult;
        }
    e.getBindingResult().getFieldError().getDefaultMessage()就是获取默认的异常信息
  • 相关阅读:
    WSGI,uwsgi,uWSGI
    彻底关闭win10自动更新
    利用python实现单向链表
    Maven的工程类型有哪些
    Maven仓库是什么
    什么是Maven
    Shiro 的优点
    比较 SpringSecurity 和 Shiro
    shiro有哪些组件
    接口绑定有几种实现方式,分别是怎么实现的
  • 原文地址:https://www.cnblogs.com/dslx/p/11316563.html
Copyright © 2011-2022 走看看