我有以下嵌套对象。我在我的控制器中使用@Valid 进行验证。这里 BindingResult 对象不验证子对象的名称字段。我错过了什么吗?
class Parent{
@Valid
private Child child;
//getter and setter for child object
}
class Child{
@NotNull(messag="Name cannot be null")
private String name;
//getter and setter for name
}
My controller validate method
@RequestMapping(value = "/validate", method = RequestMethod.POST)
public @ResponseBody String validate(@Valid @ModelAttribute("parent") Parent parent, BindingResult bindingResult) {
//Here I can see child name value if I say parent.getChild().getName()
// But if parent.getChild().getName() is null, bindingResult.hasErrors() is returning false
}