【SpringBoot】——Hibernator-Validator校验框架

介绍

首先说下大家常用的hibernate-validator,它是对JSR-303/JSR-349标准的实现,然后spring为了给开发者提供便捷集成了hibernate-validator,默认在springmvc模块。

依赖

本文所介绍皆在springboot应用的基础上,首先加上web模块:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

可以查看其子依赖,发现web模块默认使用了hibernate-validator:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
</dependency>

对实体类添加校验

public class AgentTrustor implements Serializable,UniqueVerifiableVO {

    private static final long serialVersionUID = 4095871718305603749L;

    /**
     * 主键ID
     */
    @Id
    @ApiModelProperty(value="主键ID", required = true)
    private Integer fid;

    /**
     * 代理人代码
     */
    @Length(min = 3,message = "代理人代码位数至少三位")
    @Column(name = "ftrustor_id")
    @ApiModelProperty(value="代理人代码", required = true)
    private String ftrustorId;
    /**
     * 联系人邮箱
     */
    @Email(message = "邮箱格式错误")
    @Column(name = "femail")
    @ApiModelProperty(value="联系人邮箱", required = true)
    private String femail;
}

通过注释名即可推断出校验的内容,message用作校验失败时的提示信息。

对Rest层添加校验

    @ApiOperation(value="新增", notes="")
    @PostMapping(value = "")
    //@Transactional(rollbackFor=Exception.class)
    public ObjectRestResponse<AgentTrustor> add(@RequestBody @Validated AgentTrustor agentTrustor) throws BaseException{
        agentTrustorBiz.bizInsertSelective(agentTrustor);
        return new ObjectRestResponse<AgentTrustor>().rel(true);
    }

统一异常的处理

经过对校验异常的debug发现,该异常为MethodArgumentNotValidException

MethodArgumentNotValidException

可以看到该异常对象的结构,同样我们可以根据其结构解析出想要的结果:

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public BaseResponse validExceptionHandler(HttpServletResponse response,                     MethodArgumentNotValidException ex) {
        BindingResult bindingResult = ex.getBindingResult();
        StringBuffer stringBuffer = new StringBuffer();
        if(bindingResult.hasErrors()){
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                //该格式仅仅作为response展示和log作用,前端应自己做校验
                stringBuffer.append(fieldError.getObjectName() + "--" +                             fieldError.getDefaultMessage() + " ");
            }
        }
        logger.error(stringBuffer.toString());
        return new BaseResponse(HttpStatus.BAD_REQUEST.value(),stringBuffer.toString());
    }

上面代码是统一异常处理中的一部分,主要是用来处理参数校验产生的MethodArgumentNotValidException异常。

分组校验

当我们遇到不同场景需要有不同的校验规则时候,我们可以使用分组校验。如:一个请求只校验id,一个请求只校验email:

public class AgentTrustor implements Serializable,UniqueVerifiableVO {

    private static final long serialVersionUID = 4095871718305603749L;

    /**
     * 主键ID
     */
    @Id
    @ApiModelProperty(value="主键ID", required = true)
    private Integer fid;

    /**
     * 代理人代码
     */
    @Length(min = 3,message = "代理人代码位数至少三位",groups = {ID.class})
    @Column(name = "ftrustor_id")
    @ApiModelProperty(value="代理人代码", required = true)
    private String ftrustorId;
    /**
     * 联系人邮箱
     */
    @Email(message = "邮箱格式错误",groups = {EMAIL.class})
    @Column(name = "femail")
    @ApiModelProperty(value="联系人邮箱", required = true)
    private String femail;

    public interface ID{};

    public interface EMAIL{};
}

根据需要在@Validated属性中指定需要校验的分组名,可以指定1到多个。指定到的分组名会全部进行校验,不指定的不校验

@ApiOperation(value="新增", notes="")
    @PostMapping(value = "")
    //@Transactional(rollbackFor=Exception.class)
    public ObjectRestResponse<AgentTrustor> add(@RequestBody @Validated(AgentTrustor.ID.class) AgentTrustor agentTrustor) throws BaseException{
        agentTrustorBiz.bizInsertSelective(agentTrustor);
        return new ObjectRestResponse<AgentTrustor>().rel(true);
    }

API

JSR提供的校验注解

@Null   被注释的元素必须为 null    
@NotNull    被注释的元素必须不为 null    
@AssertTrue     被注释的元素必须为 true    
@AssertFalse    被注释的元素必须为 false    
@Min(value)     被注释的元素必须是一个数字,其值必须大于等于指定的最小值    
@Max(value)     被注释的元素必须是一个数字,其值必须小于等于指定的最大值    
@DecimalMin(value)  被注释的元素必须是一个数字,其值必须大于等于指定的最小值    
@DecimalMax(value)  被注释的元素必须是一个数字,其值必须小于等于指定的最大值    
@Size(max=, min=)   被注释的元素的大小必须在指定的范围内    
@Digits (integer, fraction)     被注释的元素必须是一个数字,其值必须在可接受的范围内    
@Past   被注释的元素必须是一个过去的日期    
@Future     被注释的元素必须是一个将来的日期    
@Pattern(regex=,flag=)  被注释的元素必须符合指定的正则表达式

Hibernate Validator提供的校验注解

@NotBlank(message =)   验证字符串非null,且长度必须大于0    
@Email  被注释的元素必须是电子邮箱地址    
@Length(min=,max=)  被注释的字符串的大小必须在指定的范围内    
@NotEmpty   被注释的字符串的必须非空    
@Range(min=,max=,message=)  被注释的元素必须在合适的范围内

原文地址:https://blog.csdn.net/weixin_37509652/article/details/80676693

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值