Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javax.validation.valid on child objects

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?

like image 691
shha Avatar asked Aug 10 '17 10:08

shha


People also ask

How does javax validation valid work?

validation will validate the nested object for constraints with the help of javax. validation implementation provider, for example, hibernate validator. @Valid also works for collection-typed fields. In the case of collection-typed fields, each collection element will be validated.

What is the difference between javax validation and Hibernate validator?

The Javax bean validation API provides the following most frequently used annotations. The Hibernate validator provides the following commonly used annotations for validation. In case of product or project development we must use both the annotations for bean validation.

What is difference between @valid and @validated?

The @Valid annotation ensures the validation of the whole object. Importantly, it performs the validation of the whole object graph. However, this creates issues for scenarios needing only partial validation. On the other hand, we can use @Validated for group validation, including the above partial validation.

What is the use of @validated annotation?

The @Validated annotation is a class-level annotation that we can use to tell Spring to validate parameters that are passed into a method of the annotated class. We'll learn more about how to use it in the section about validating path variables and request parameters.


1 Answers

Just Add @Valid to your childList and it should work

public class Parent {
@Size(min = 5, max = 10)
String lastName;
@Valid
List<Child> childs;

}

like image 119
李骏豪 Avatar answered Sep 24 '22 02:09

李骏豪