Hibernate Validator是Hibernate提供的一个开源框架,使用注解方式非常方便的实现服务端的数据校验。

官网:http://hibernate.org/validator/

hibernate Validator 是 Bean Validation 的参考实现 。

Hibernate Validator 提供了 JSR 303 规范中所有内置 constraint(约束) 的实现,除此之外还有一些附加的 constraint。

在日常开发中,Hibernate Validator经常用来验证bean的字段,基于注解,方便快捷高效。

Bean校验的注解:

添加maven依赖:

<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>

对象属性约束:

@Table(name = "tb_user")
@Data
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; @NotEmpty(message = "用户名不能为空")
@Length(min = 4, max = 15, message = "用户名长度必须是4~15位")
private String username;// 用户名 @JsonIgnore
@NotEmpty(message = "密码不能为空")
@Length(min = 6, max = 25, message = "密码长度必须是6~25位")
private String password;// 密码 @Pattern(regexp = "^1[35678]\\d{9}$", message = "手机号格式不正确")
private String phone;// 电话 private Date created;// 创建时间 @JsonIgnore
private String salt;// 密码的盐值 }

使约束生效:

@PostMapping("/register")
public ResponseEntity<Void> register(@Valid User user, @RequestParam("code") String code) {
userService.register(user, code);
return ResponseEntity.status(HttpStatus.CREATED).build();
}

Java Hibernate Validator的更多相关文章

  1. org.hibernate.validator.constraints.NotBlank' validating type 'java.lang.Integer

    使用hibernate时,在save方法时,报了:org.hibernate.validator.constraints.NotBlank' validating type 'java.lang.In ...

  2. Java笔记 #07# Hibernate Validator

    Hibernate Validator是Spring Boot默认附带的标准校验API(javax.validation)实现. 应用实例(配合切面) 采用注解定义切面.java @Aspect @C ...

  3. hibernate 解决 java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.validator.internal.engine.xxx 这类的问题

    <!-- 解决 java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.validator.intern ...

  4. hibernate.validator.constraints.NotEmpty校验请求参数报错java.lang.NoClassDefFoundError: javax/el/PropertyNotFoundException

    spring maven项目,使用hibernate validator 注解形式校验客户端的请求参数. hibernate-validator版本:5.0.2.Final validation-ap ...

  5. Caused by: java.lang.AbstractMethodError: org.hibernate.validator.internal.engine.ConfigurationImpl

    1.错误描述 严重: StandardWrapper.Throwable org.springframework.beans.factory.BeanCreationException: Error ...

  6. java.lang.NoClassDefFoundError: org/hibernate/validator/internal/engine/DefaultClockProvider

    ①在springboot的spring-boot-starter-web默认引入了以下依赖: <dependency> <groupId>com.fasterxml.jacks ...

  7. spring boot 1.4默认使用 hibernate validator

    spring boot 1.4默认使用 hibernate validator 5.2.4 Final实现校验功能.hibernate validator 5.2.4 Final是JSR 349 Be ...

  8. JSR303的数据校验-Hibernate Validator方式实现

    1.什么是JSR303? JSR303是java为bean数据合法性校验所提供的一个标准规范,叫做Bean Validation. Bean Validation是一个运行时的数据校验框架,在验证之后 ...

  9. Hibernate Validator 6.0.9.Final - JSR 380 Reference Implementation: Reference Guide

    Preface Validating data is a common task that occurs throughout all application layers, from the pre ...

随机推荐

  1. 如何用kindle看论文

    kindle的pdf显示效果向来不好,那么如何把pdf转换成mobi格式呢 可以把个人文件(pdf)发送至您的[发送至Kindle]电子邮箱时,在电子邮件的主题栏中填写"convert&qu ...

  2. order by 多条件查询 case when

    场景:在按照条件查询后,排序按照不同的条件排序,以及同一个条件 正序和倒序排序.可以考虑使用. 遇到的排序条件:按照直播的状态,根据条件排序.直播的状态包括:直播.置顶.预告.回放.过期预告.排序条件 ...

  3. 京东原来你运用的这玩意,不错,我也要!! ContainerDNS

    转自社区 ContainerDNS 本文介绍的 DNS 命名为 ContainerDNS,作为京东商城软件定义数据中心的关键基础服务之一,具有以下特点: 分布式,高可用 自动发现服务域名 后端探活 易 ...

  4. Android SDK和ADT无法更新的解决办法

    重要的东西要标红: 经过另外一台电脑测试,按照第二步设置好hosts后,可以一并解决 SDK 和 ADT的更新,无需单独设置SDK Manager. 这里还是写出如何设置SDK Manager以作记录 ...

  5. Visual Studio 2017 连接Oracle

    VS 2017 连接 Oracle 12 因为Visual Studio自带的数据文件已经不能支持超过10g以上的了,所以需要另外 下载插件 本机环境 宿主机的环境:win7,Visual Studi ...

  6. [BZOJ 1124][POI 2008] 枪战 Maf

    1124: [POI2008]枪战Maf Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 659  Solved: 259[Submit][Status ...

  7. 11、Node.js 函数

    内容:普通函数,匿名函数,函数传递是如何让HTTP服务器工作的 ###普通函数例子: function say(word) { console.log(word); } function execut ...

  8. Production-Ready Beanstalkd with Laravel 4 Queues

    原文地址:http://fideloper.com/ubuntu-beanstalkd-and-laravel4 Note: TL;DR version at the bottom! Queues a ...

  9. update from select

    CREATE TABLE dualx( x_id ) NOT NULL , x_con ) ) CREATE TABLE dualy( y_id ) NOT NULL , y_con ) ) ','x ...

  10. fill & stroke

    - (void)stroke Draws a line along the receiver’s path using the current drawing properties. - (void) ...