我想验证地址列表及其所有属性 city, state , pincode 我已经在 Address 类中进行了所有 spring 验证,但它仍然没有验证。
地址类
@Entity
@Table(name = "ADDRESS")
public class Address {
@Id
@GeneratedValue
@Column(name = "ADDR_ID")
private Long addr_id;
@NotNull(message="city should not be null")
@Column(name = "CITY")
private String city;
@NotNull(message="state should not be null")
@Column(name = "STATE")
private String state;
@NotNull(message="pincode should not be null")
@Column(name = "PINCODE")
private Integer pincode;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "FK_EMP_ID", referencedColumnName="ID")
@JsonBackReference
@Valid
@NotNull
private Employee emp;
//getter & setter omitted for breveity
员工类
@Entity
@Table(name = "EMPLOYEE")
public class Employee {
@Id
@GeneratedValue
@Column(name = "ID")
private Long id;
@NotNull(message="name should not be null")
@Size(min=1, max=15 ,message="Name should be min 1 and max 15")
@Column(name = "EMPLOYEE_NAME")
private String name;
@NotNull(message="salary should not be null")
@Column(name = "EMPLOYEE_SALARY")
private Integer salary;
@NotNull(message="department should not be null")
@Column(name = "DEPARTMENT")
private String department;
@OneToMany(mappedBy="emp", cascade={CascadeType.ALL},
orphanRemoval=true,fetch=FetchType.LAZY)
@JsonManagedReference
@NotNull(message="address should contain atleast 1 address")
private List<Address> address = new ArrayList<Address>();
//getter & setter omitted for breveity
邮递员 Json
{
"name": "aakash",
"salary": 546,
"department": "IT",
"age":32,
"email":"agag@gma",
"address":[
{
"city":"Mumbai"
}
]
}
当我传递地址列表及其一些属性时,它显示以下错误。
javax.validation.ConstraintViolationException: Validation failed for classes
[com.example.springDataJPA.model.Address] during persist time for groups
[javax.validation.groups.Default, ]
List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='pincode should not be null',
propertyPath=pincode, rootBeanClass=class
com.example.springDataJPA.model.Address, messageTemplate='pincode should not
be null'}
ConstraintViolationImpl{interpolatedMessage='state should not be null',
propertyPath=state, rootBeanClass=class
com.example.springDataJPA.model.Address, messageTemplate='state should not
be null'}
我想验证具有所有属性的地址列表,并且应该并且它应该以 json 格式返回正确的默认消息。