假设我有以下pojo和约束:
public class Pojo1 {
@NotNull
private String field1;
@NotNull
private String field2; // <--- I only want this NotNull in Pojo3.java but not in Pojo2.java
}
public class Pojo2 {
@Valid
private Pojo1 pojo1;
}
public class Pojo3 {
@Valid
private Pojo1 pojo1;
@Valid
private Pojo2 pojo2;
}
我想要Pojo1。仅在Pojo3下验证field2,而不是在Pojo2下验证。换句话说,以下JSON应该通过验证
{
"pojo1": {
"field1": "something",
"field2": "something"
},
"pojo2": {
"pojo1": {
"field1": "something"
}
}
}
尝试替换Pojo1。在Pojo3上使用自定义验证的field2验证,例如:
public class Pojo1 {
@NotNull
private String field1;
// @NotNull // don't validate it generally
private String field2;
}
public class Pojo2 {
@Valid
private Pojo1 pojo1;
}
public class Pojo3 {
@Valid
private Pojo1 pojo1;
@Valid
private Pojo2 pojo2;
@javax.validation.constraints.AssertTrue
public boolean isPojo1Valid() {
return pojo1 == null || pojo1.getField2() != null;
}
}