I've got a simple structure
public class Parent {
@Size(min = 5, max = 10)
String lastName;
List<Child> childs;
}
public class Child {
@Size(min = 5, max = 10)
String firstName;
@Size(min = 1, max = 18)
Integer age;
}
and a method with definition
@ResponseBody
@RequestMapping(method = RequestMethod.POST, consumes = "application/json", produces="application/json")
public RestResponse create(@RequestBody Parent object)
which creates (and gives a possibility to update) parent objects.
Let's assume that I'd like to create two functionalities:
1) To create a working copy - user doesn't have to insert correctly all of the data.
2) To send me this data - in this example I'd like to valid if they're correct.
I've found that there is a way to valid it by adding @Valid annotation to the parameter in method definition, like that:
@ResponseBody
@RequestMapping(method = RequestMethod.POST, consumes = "application/json", produces="application/json")
public RestResponse create(@RequestBody @Valid Parent object)
but it do valid only parent field, skipping childs. I can add @Valid annotation on child list:
public class Parent {
@Size(min = 5, max = 10)
String lastName;
List<Child> childs;
}
but it'll work all the time - even when I'd like to save a working copy. Is it possible to valid objects inside parent object conditionally?