一直在犹豫要不要写一些经常会遇到的一些异常情况,直到最近引用同事jar包老是报错,才下定决心对异常做一下总结。

先拿一段错误的代码看看:

@Min(value = 0, message = "DD_VALIDATION_1009")
private Float cargo_price;

通过Hibernate-validator校验参数是一种十分常见的检验方式,这里就不多做介绍,直入主题,对异常进行分析。

om.alibaba.dubbo.rpc.RpcException: Failed to invoke the method postOrder in the service com.xkeshi.delivery.apis.DaDaOrderDubboService. Tried 1 times of the providers [192.168.184.15:20887] (1/1) from the registry 192.168.184.10:2181 on the consumer 192.168.184.15 using the dubbo version 2.5.4. Last error is: HV000030: No validator could be found for type: java.lang.Float.

关键字:HV000030: No validator could be found for type: java.lang.Float.

由于是调用的dubbo接口,因此给出的是RpcException,但是重点在于 HV这个关键字。HV是Hibernate-validator的异常,在注解有异常的时候会抛出。

下面是主要的验证注解及说明:

注解

适用的数据类型

说明

@AssertFalse

Boolean, boolean

验证注解的元素值是false

@AssertTrue

Boolean, boolean

验证注解的元素值是true

@DecimalMax(value=x)

BigDecimal, BigInteger, String, byte,short, int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of Number andCharSequence.

验证注解的元素值小于等于@ DecimalMax指定的value值

@DecimalMin(value=x)

BigDecimal, BigInteger, String, byte,short, int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of Number andCharSequence.

验证注解的元素值小于等于@ DecimalMin指定的value值

@Digits(integer=整数位数, fraction=小数位数)

BigDecimal, BigInteger, String, byte,short, int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of Number andCharSequence.

验证注解的元素值的整数位数和小数位数上限

@Future

java.util.Date, java.util.Calendar; Additionally supported by HV, if theJoda Time date/time API is on the class path: any implementations ofReadablePartial andReadableInstant.

验证注解的元素值(日期类型)比当前时间晚

@Max(value=x)

BigDecimal, BigInteger, byte, short,int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type ofCharSequence (the numeric value represented by the character sequence is evaluated), any sub-type of Number.

验证注解的元素值小于等于@Max指定的value值

@Min(value=x)

BigDecimal, BigInteger, byte, short,int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of CharSequence (the numeric value represented by the char sequence is evaluated), any sub-type of Number.

验证注解的元素值大于等于@Min指定的value值

@NotNull

Any type

验证注解的元素值不是null

@Null

Any type

验证注解的元素值是null

@Past

java.util.Date, java.util.Calendar; Additionally supported by HV, if theJoda Time date/time API is on the class path: any implementations ofReadablePartial andReadableInstant.

验证注解的元素值(日期类型)比当前时间早

@Pattern(regex=正则表达式, flag=)

String. Additionally supported by HV: any sub-type of CharSequence.

验证注解的元素值与指定的正则表达式匹配

@Size(min=最小值, max=最大值)

String, Collection, Map and arrays. Additionally supported by HV: any sub-type of CharSequence.

验证注解的元素值的在min和max(包含)指定区间之内,如字符长度、集合大小

@Valid

Any non-primitive type(引用类型)

验证关联的对象,如账户对象里有一个订单对象,指定验证订单对象

@NotEmpty

CharSequence,CollectionMap and Arrays

验证注解的元素值不为null且不为空(字符串长度不为0、集合大小不为0)

@Range(min=最小值, max=最大值)

CharSequence, Collection, Map and Arrays,BigDecimal, BigInteger, CharSequence, byte, short, int, long and the respective wrappers of the primitive types

验证注解的元素值在最小值和最大值之间

@NotBlank

CharSequence

验证注解的元素值不为空(不为null、去除首位空格后长度为0),不同于@NotEmpty,@NotBlank只应用于字符串且在比较时会去除字符串的空格

@Length(min=下限, max=上限)

CharSequence

验证注解的元素值长度在min和max区间内

@Email

CharSequence

验证注解的元素值是Email,也可以通过正则表达式和flag指定自定义的email格式

Hibernate-validator(HV)异常相关推荐

  1. SpringMVC集成Hibernate Validator进行注解式的参数校验——让代码更少、更加专注于业务逻辑

    SpringMVC集成Hibernate Validator进行注解式的参数校验 --让代码更少.更加专注于业务逻辑 1 问题背景: 参数验证是一个常见的问题,例如验证用户输入的密码是否为空.邮箱是否 ...

  2. springboot使用hibernate validator校验

    回到顶部 一.参数校验 在开发中经常需要写一些字段校验的代码,比如字段非空,字段长度限制,邮箱格式验证等等,写这些与业务逻辑关系不大的代码个人感觉有两个麻烦: 验证代码繁琐,重复劳动 方法内代码显得冗 ...

  3. Springmvc的服务端数据验证-----Hibernate Validator

    导入Hibernate validator的Jar包 hibernate-validator-4.3.0.Final.jar jboss-logging-3.1.0.CR2.jar validatio ...

  4. SpringBoot中使用Hibernate Validator校验工具类

    1.说明 在Spring Boot已经集成Hibernate Validator校验器的情况下, 对于配置了校验注解的请求参数, 框架会自动校验其参数, 但是如果想手动校验一个加了注解的普通对象, 比 ...

  5. hibernate.validator验证参数

    前言 在接口开发中,经常是需要对传入参数完整性进行验证的,或者对手机号格式进行验证等,而一般不了解@Valid注解之前,都是通过自己封装方法来验证,都是一堆if else集合,这样判断对于代码结构很不 ...

  6. Hibernate Validator 后台数据规则校验拓展

    目录 1.dto 1-1.UserInfo 2.rules 2-1.after 2-1-1.UserInfoAfterRuleValid.java 2-2.before 2-2-1.UserInfoB ...

  7. springboot中hibernate validator校验模式,分组校验,自定义校验

    检验模式 上面例子中一次性返回了所有验证不通过的集合,通常按顺序验证到第一个字段不符合验证要求时,就可以直接拒绝请求了.Hibernate Validator有以下两种验证模式: 普通模式(默认是这个 ...

  8. Spring Boot集成Hibernate Validator

    废话不多说,直接开始集成环境. 一.环境集成 在项目中hibernate-Validator包在spring-boot-starter-web包里面有,不需要重复引用 .(整个Demo都是用PostM ...

  9. 如何优雅的做数据校验-Hibernate Validator详细使用说明

    文章目录 Hibernate Validator的使用 依赖 bean约束声明和验证,Validator 方法约束声明和验证,ExecutableValidator 约束注解 空与非空检查 Boole ...

  10. Hibernate Validator

    2019独角兽企业重金招聘Python工程师标准>>> 本文主要讲述如何使用hibernate validator来校验入参,避免在业务代码里进行每个接口进行入参校验,提搞代码的简洁 ...

最新文章

  1. 使用 PHPMailer 发送邮件
  2. mvp的全称_现役最强外援,总决赛MVP,来到CBA之后赚了多少钱?
  3. 九. Python基础(9)--命名空间, 作用域
  4. 初步学习大数据——设置虚拟机固定ip地址
  5. CentOS最小化安装后AR8151网卡驱动未安装解决办法
  6. 【WIN10】WIN2D——基本圖形的繪製
  7. UVA 10405-Longest Common Subsequence
  8. nginx https透明代理_Nginx反向代理https,配置lets-encrypt证书教程
  9. Flink on Hive构建流批一体数仓
  10. java shapefile 中文乱码_GeoTools操作Shape格式文件
  11. 实时监控 轻松玩转IT运维
  12. 多域名证书的签名和自签名流程
  13. Oracle数据库学习笔记(四)--Oracle体系结构
  14. Mac版Permute 3(万能视频转换器)
  15. ZBrush雕刻人物:人体比例及肌肉骨骼介绍
  16. nginx开启Gzip压缩
  17. 大学四年,因为这40个开发工具,我成为别人眼中的大神
  18. winform遍历bartender_C# 调用Bartender服务并打印bartender标签
  19. 软考和PMP认证对比,哪个含金量高?
  20. git学习之:如何将远程代码强制拉取同步(覆盖)到本地文件夹

热门文章

  1. Linux WAK介绍与入门实例
  2. 安卓手机如何在线预览PDF文档
  3. 【渝粤教育】国家开放大学2018年春季 8621-22T劳动合同法 参考试题
  4. C++文件操作API函数介绍
  5. 半年,她从打工妹到小老板!
  6. 【美股】美股中的几种分析形态
  7. 行为驱动开发(BDD)你准备好了吗?
  8. 移动端页面调用微信支付
  9. matlab 求集中度,matlab代写使用Copula仿真优化市场风险数据VaR分析
  10. 删除小红点\u200b的