jar包
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.1.3.Final</version> </dependency>
在实体类上加注解
package com.mytaotao.sso.bean; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.validator.constraints.Length; import com.fasterxml.jackson.annotation.JsonIgnore; import com.mytaotao.bean.BaseTime; @Table(name="tb_users") public class Users extends BaseTime{ @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id; @Length(max=20,min=6,message="用户长度为6-20位") private String username; @Length(max=20,min=6,message="密码长度为6-20位") @JsonIgnore private String password; @Length(max=11,min=11,message="手机长度为11位") private String phone; private String email; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }