栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java参数合法性校验(Hibernate Validator)及全局异常处理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java参数合法性校验(Hibernate Validator)及全局异常处理

作者:不染心

时间:2022/6/5

源码地址:https://download.csdn.net/download/qq_38234785/85557796

文章目录
  • 简介
  • Java参数合法性校验(Hibernate Validator)
    • :four_leaf_clover: **UserController.java**
    • :four_leaf_clover: **User.java**
    • :four_leaf_clover: **PatternContents.java**
  • Java全局异常处理
    • :four_leaf_clover: **CodeMsgException.java**
    • :four_leaf_clover: **GlobalException.java**
    • :four_leaf_clover: **GlobalExceptionHandler.java**

简介

在程序开发中,经常要对数据进行校验后再进行业务处理。如果在业务代码中进行Bean的字段校验,会使得代码变的十分繁琐。JSR303 - Bean Validation 为JavaBean的验证定义了相关的元数据模型和API。

This JSR will define a meta-data model and API for JavaBeanTM validation based on annotations, with overrides and extended meta-data through the use of XML validation descriptors.

在 java 8 之后,又推出了JSR380 - Bean Validation 2.0。

This JSR aims at evolving the Bean Validation specification by leveraging Java 8 language constructs for the purposes of validation.

Hibernate Validator 是 Bean Validation 的实现,Hibernate Validator 内置了 JSR303/JSR380 中所有的 constraint 的实现,还额外提供了很多自定义的 constraint。

项目目录结果如下:
    |--- controller
        |--- UserController
    |--- entity
        |--- CodeMsgException
        |--- GlobalException
        |--- User
    |--- interceptor
        |--- GlobalExceptionHandler
    |--- Utils
        |--- PatternContents

导入本项目依赖:

    
        org.springframework.boot
        spring-boot-starter-web
    

    
        org.projectlombok
        lombok
        true
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    


    
    
        org.hibernate.validator
        hibernate-validator
        6.1.7.Final
    
    
        javax.el
        javax.el-api
        3.0.0
    
    
Java参数合法性校验(Hibernate Validator)  UserController.java
package com.example.valitedpro.controller;


import com.example.valitedpro.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



@Slf4j
@Validated
@RestController
@RequestMapping("/api")
public class UserController {

    @PostMapping("/add")
    public String addSysUser(@Validated User user) throws Exception {
        log.info("开始测试参数校验功能");
        return "validator参数校验";
    }
}
 User.java
package com.example.valitedpro.entity;


import com.example.valitedpro.Utils.PatternContents;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.ToString;

import javax.validation.constraints.*;



@Data
@ToString
@AllArgsConstructor
public class User {
        
    @Pattern(regexp = PatternContents.SYSUSER_NAME_TYPE, message = PatternContents.SYSUSER_NAME_DES)
    private String user_name;


    @Size(min = 5, max = 20, message = "pass太短了")
    private String user_pass;
}
 PatternContents.java
package com.example.valitedpro.Utils;


public class PatternContents {
    
    private static final String V_INTEGER = "^-?[1-9]\d*$";

    
    private static final String V_POSITIVE_INTEGER = "^[1-9]\d*$";

    
    private static final String V_NEGATIVE_INTEGER = "^-[1-9]\d*$";

    
    private static final String V_NUMBER = "^([+-]?)\d*\.?\d+$";

    
    private static final String V_POSITIVE_NUMBER = "^[1-9]\d*|0$";

    
    private static final String V_NEGATINE_NUMBER = "^-[1-9]\d*|0$";

    
    private static final String V_FLOAT = "^([+-]?)\d*\.\d+$";

    
    private static final String V_POSTTIVE_FLOAT = "^[1-9]\d*.\d*|0.\d*[1-9]\d*$";

    
    private static final String V_NEGATIVE_FLOAT = "^-([1-9]\d*.\d*|0.\d*[1-9]\d*)$";

    
    private static final String V_UNPOSITIVE_FLOAT = "^[1-9]\d*.\d*|0.\d*[1-9]\d*|0?.0+|0$";

    
    private static final String V_UNNEGATIVE_FLOAT = "^(-([1-9]\d*.\d*|0.\d*[1-9]\d*))|0?.0+|0$";

    
    private static final String V_EMAIL = "^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$";

    
    private static final String V_COLOR = "^[a-fA-F0-9]{6}$";

    
    private static final String V_URL = "^http[s]?:\/\/([\w-]+\.)+[\w-]+([\w-./?%&=]*)?$";

    
    private static final String V_CHINESE = "^[\u4E00-\u9FA5\uF900-\uFA2D]+$";

    
    private static final String V_ASCII = "^[\x00-\xFF]+$";

    
    private static final String V_ZIPCODE = "^\d{6}$";

    
    public static final String V_MOBILE = "^(1)[0-9]{10}$";

    
    private static final String V_IP4 = "^(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)$";

    
    private static final String V_NOTEMPTY = "^\S+$";

    
    private static final String V_PICTURE = "(.*)\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$";

    
    private static final String V_RAR = "(.*)\.(rar|zip|7zip|tgz)$";

    
    private static final String V_DATE = "^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$";

    
    private static final String V_QQ_NUMBER = "^[1-9]*[1-9][0-9]*$";

    
    private static final String V_TEL = "^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$";

    
    private static final String V_USERNAME = "^\w+$";

    
    private static final String V_LETTER = "^[A-Za-z]+$";

    
    private static final String V_LETTER_UPPER = "^[A-Z]+$";

    
    private static final String V_LETTER_LOWER = "^[a-z]+$";

    
    private static final String V_IDCARD = "^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$";

    
    private static final String V_PASSWORD_REG = "[A-Za-z]+[0-9]";

    
    private static final String V_PASSWORD_LENGTH = "^\d{6,18}$";

    
    private static final String V_TWO_POINT = "^[0-9]+(.[0-9]{2})?$";

    
    private static final String V_31DAYS = "^((0?[1-9])|((1|2)[0-9])|30|31)$";


    
    // 手机号
    public static final String PHONE_TYPE = "^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$";
    public static final String PHONE_DES = "电话号码不合法";

    // email
    public static final String EMAIL_TYPE = "^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";
    public static final String EMAIL_DES = "Email不合法";

    // 身份证号码
    public static final String IDCARD_TYPE = "^\d{15}|\d{18}$";
    public static final String IDCARD_DES = "身份证不合法";

    // 中文名字
    public static final String CHINESE_NAME_TPYE = "^[\u4e00-\u9fa5]{2,}$";
    public static final String CHINESE_NAME_DES = "长度大于2的中文名字";

    
    // 账户名称
    public static final String SYSUSER_NAME_TYPE = "^.{8,}$";
    public static final String SYSUSER_NAME_DES = "用户名称长度应大于8";

    // 账户密码
    public static final String SYSUSER_PASS_TYPE = "^(\w){6,20}$";
    public static final String SYSUSER_PASS_DES = "用户密码长度应大于6";


    
    public static final String ROLE_CODE = "^[A-Za-z]{2,}$";
    public static final String ROLE_DESCRIPTION = "^.{2,}$";

}

Java全局异常处理

在项目中异常处理是与用户交互,提升系统友好性的重要手段,尤为重要。代码中每次出现异常的地方都需要try catch则会出现大量的冗余代码,不方便代码的统一管理。所以,我们在项目中配置一个异常拦截器,在其中对异常进行统一处理,不需要写那么多try catch。

 CodeMsgException.java
package com.example.valitedpro.entity;

import java.io.Serializable;



public class CodeMsgException implements Serializable {
    private int code;
    private String msg;

    //通用异常
    public static CodeMsgException SUCCESS = new CodeMsgException(0, "success");
    public static CodeMsgException SERVER_ERROR = new CodeMsgException(500100, "服务端异常");
    //注意  %s ,格式化字符串
    public static CodeMsgException SERVER_BIND_ERROR = new CodeMsgException(500101, "服务端绑定异常:%s");
    //登录模块 5002XX
    public static CodeMsgException MSG_NAME_IS_EMPTY = new CodeMsgException(500200, "用户名不能为空!");
    public static CodeMsgException MSG_PASSWORD_IS_EMPTY = new CodeMsgException(500201, "密码不能为空!");
    public static CodeMsgException MSG_MOBILE_ERROR = new CodeMsgException(500202, "手机号格式不正确!");
    public static CodeMsgException MSG_MOBILE_IS_EMPTY = new CodeMsgException(500203, "手机号不能为空!");
    public static CodeMsgException MSG_MOBILE_NOT_EXIST = new CodeMsgException(500204, "手机号不存在!");
    public static CodeMsgException MSG_PASSWORD_ERROR = new CodeMsgException(500205, "密码错误!");

    //权限模块 5003XX
    public static CodeMsgException MSG_PERMISSION_NEED_ADMIN = new CodeMsgException(500301, "需要管理员权限!");


    private CodeMsgException(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    public CodeMsgException fillArgs(Object ... args){
        int code=this.code;
        String message=String.format(msg,args);
        return new CodeMsgException(code,message);
    }
    public int getCode() {
        return code;
    }
    public String getMsg() {
        return msg;
    }
}

 GlobalException.java
package com.example.valitedpro.entity;

import java.io.Serializable;


public class GlobalException extends RuntimeException implements Serializable {
    private CodeMsgException codeMsgException;
    public GlobalException(CodeMsgException codeMsgException){
        super(codeMsgException.toString());
        this.codeMsgException = codeMsgException;

    }

    public CodeMsgException getCodeMsg() {
        return codeMsgException;
    }
}

 GlobalExceptionHandler.java
package com.example.valitedpro.interceptor;




import com.example.valitedpro.entity.CodeMsgException;
import com.example.valitedpro.entity.GlobalException;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;



@ControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody
public class GlobalExceptionHandler {
    @ExceptionHandler({Exception.class, BindException.class})
    public CodeMsgException exceptionHandler(HttpServletRequest request, Exception e) {
        System.out.println(e);
        // 本地自定义异常处理
        if(e instanceof GlobalException){
            return ((GlobalException) e).getCodeMsg();
        }
        //绑定异常是需要明确提示给用户的
        else if (e instanceof BindException) {
            BindException exception = (BindException) e;
            List errors = exception.getAllErrors();
            //获取自错误信息
            String msg = errors.get(0).getDefaultMessage();
            //将具体错误信息设置到CodeMsg中返回
            return CodeMsgException.SERVER_BIND_ERROR.fillArgs(msg);
        }
        // 系统异常处理
        return CodeMsgException.SERVER_BIND_ERROR.fillArgs(e.getCause() + e.getMessage());
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/952455.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号