所以我想说我有像这样的用户对象
@Entity
public class User {
@Id
@GeneratedValue
private long id;
private String name;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "address", referencedColumnName = "id")
private Address address;
}
@Entity
public class Address {
@Id
@GeneratedValue
private long id;
private String city;
private String country;
}
现在我不想在实体中编写验证注释。我想要做的是在User
中验证@RestController
,就像这样
@RestController
public class InvoiceController {
@RequestMapping(value="/users/add", method = RequestMethod.POST)
public Invoice addInvoice(@Validated @RequestBody ValidUser user) {
... do stuff
}
}
验证注释将在ValidUser
中,就像这样。
public class ValidUser extends User {
@NotNull
private String name;
@Valid
private Address address;
}
public class ValidAddress extends Address{
@NotNull
private String city;
@NotNull
private String country;
}
当我从address
中删除ValidUser
字段时,验证工作正常,但在fileURL
时却没有。如何使地址验证也有效?