What does the @Valid annotation indicate in Spring?

In the following example, the ScriptFile parameter is marked with an @Valid annotation. What does @Valid annotation do?

@RequestMapping(value = "/scriptfile", method = RequestMethod.POST) public String create(@Valid ScriptFile scriptFile, BindingResult result, ModelMap modelMap) < if (scriptFile == null) throw new IllegalArgumentException("A scriptFile is required"); if (result.hasErrors()) < modelMap.addAttribute("scriptFile", scriptFile); modelMap.addAttribute("showcases", ShowCase.findAllShowCases()); return "scriptfile/create"; >scriptFile.persist(); return "redirect:/scriptfile/" + scriptFile.getId(); > 
15.6k 18 18 gold badges 60 60 silver badges 88 88 bronze badges asked Aug 29, 2010 at 14:42 1,419 2 2 gold badges 10 10 silver badges 10 10 bronze badges

9 Answers 9

It's for validation purposes.

Validation It is common to validate a model after binding user input to it. Spring 3 provides support for declarative validation with JSR-303. This support is enabled automatically if a JSR-303 provider, such as Hibernate Validator, is present on your classpath. When enabled, you can trigger validation simply by annotating a Controller method parameter with the @Valid annotation: After binding incoming POST parameters, the AppointmentForm will be validated; in this case, to verify the date field value is not null and occurs in the future.

16.3k 11 11 gold badges 77 77 silver badges 108 108 bronze badges answered Aug 29, 2010 at 14:51 16.8k 17 17 gold badges 56 56 silver badges 65 65 bronze badges

To make a long story short, in your particular example code, ScriptFile class has some constraints defined for the class data members like @NotNull , @NotEmpty , etc. and @Valid instructs the framework (spring in our case) to check these constraints against the parameter supplied when someone calls your method. In this case, if the validation fails, the server responds with an HTTP 400 Bad Request status code.

Commented Oct 19, 2021 at 13:17 @EmreTapcı what will happen if we don't put valid? Won't it validate any constrain? Commented Jun 4, 2022 at 9:03 What happens if the the entity marked with @Valid is null? Commented Aug 31, 2022 at 15:32

Adding to above answers, take a look at following. AppointmentForm 's date column is annotated with couple of annotations. By having @Valid annotation that triggers validations on the AppointmentForm (in this case @NotNull and @Future ). These annotations could come from different JSR-303 providers (e.g, Hibernate, Spring..etc).

 @RequestMapping(value = "/appointments", method = RequestMethod.POST) public String add(@Valid AppointmentForm form, BindingResult result) < . >static class AppointmentForm
782 1 1 gold badge 10 10 silver badges 23 23 bronze badges answered Oct 7, 2015 at 0:31 Charith De Silva Charith De Silva 3,690 4 4 gold badges 44 44 silver badges 48 48 bronze badges

I have a similar code. I had removed @Valid on ApplicationForm parameter but, still validations were fired on date (set as null )field. Please explain.

Commented Jan 10, 2019 at 6:06

I wanted to add more details about how the @Valid works, especially in spring.

Everything you'd want to know about validation in spring is explained clearly and in detail in https://reflectoring.io/bean-validation-with-spring-boot/, but I'll copy the answer to how @Valid works incase the link goes down.

The @Valid annotation can be added to variables in a rest controller method to validate them. There are 3 types of variables that can be validated:

So now. how does spring "validate"? You can define constraints to the fields of a class by annotating them with certain annotations. Then, you pass an object of that class into a Validator which checks if the constraints are satisfied.

For example, suppose I had controller method like this:

@RestController class ValidateRequestBodyController < @PostMapping("/validateBody") ResponseEntityvalidateBody(@Valid @RequestBody Input input) < return ResponseEntity.ok("valid"); >> 

So this is a POST request which takes in a request body, and we're mapping that request body to a class Input .

Here's the class Input :

class Input < @Min(1) @Max(10) private int numberBetweenOneAndTen; @Pattern(regexp = "^[0-9]\\.[0-9]\\.[0-9]\\.[0-9]$") private String ipAddress; // . > 

The @Valid annotation will tell spring to go and validate the data passed into the controller by checking to see that the integer numberBetweenOneAndTen is between 1 and 10 inclusive because of those min and max annotations. It'll also check to make sure the ip address passed in matches the regular expression in the annotation.

side note: the regular expression isn't perfect.. you could pass in 3 digit numbers that are greater than 255 and it would still match the regular expression.

Here's an example of validating a query variable and path variable:

@RestController @Validated class ValidateParametersController < @GetMapping("/validatePathVariable/") ResponseEntity validatePathVariable( @PathVariable("id") @Min(5) int id) < return ResponseEntity.ok("valid"); >@GetMapping("/validateRequestParameter") ResponseEntity validateRequestParameter( @RequestParam("param") @Min(5) int param) < return ResponseEntity.ok("valid"); >> 

In this case, since the query variable and path variable are just integers instead of just complex classes, we put the constraint annotation @Min(5) right on the parameter instead of using @Valid .

answered Dec 8, 2020 at 15:45 1,872 1 1 gold badge 22 22 silver badges 19 19 bronze badges

> So this is a POST request which takes in a response body, and we're mapping that response body to a class Input. shouldn't it be POST request which takes in a "request body" ?

Commented Apr 15, 2021 at 18:29 @sss: yeah, that was a mistake. It's been corrected now Commented Apr 16, 2021 at 21:01

@SomeGuy I'm just wondering what would it result if 1. I send the value as 'null' - @ Valid @ RequestBody Input input 2. I pass the value as 0 for numberBetweenOneAndTen

Commented May 29, 2023 at 19:43

@EnthuCoder: I'm not sure actually. I think null is considered valid, and if you don't want null to be considered valid, you'd have to use @ NonNull

Commented May 30, 2023 at 17:48

@Valid in itself has nothing to do with Spring. It's part of Bean Validation specification(there are several of them, the latest one being JSR 380 as of second half of 2017), but @Valid is very old and derives all the way from JSR 303.

As we all know, Spring is very good at providing integration with all different JSRs and java libraries in general(think of JPA, JTA, Caching, etc.) and of course those guys took care of validation as well. One of the key components that facilitates this is MethodValidationPostProcessor.

Trying to answer your question - @Valid is very handy for so called validation cascading when you want to validate a complex graph and not just a top-level elements of an object. Every time you want to go deeper, you have to use @Valid . That's what JSR dictates. Spring will comply with that with some minor deviations(for example I tried putting @Validated instead of @Valid on RestController method and validation works, but the same will not apply for a regular "service" beans).

answered Nov 9, 2017 at 12:56 9,485 9 9 gold badges 61 61 silver badges 68 68 bronze badges But, what does validate? Commented Jun 9, 2020 at 18:13 Please elaborate, @nephewtom. What do you want to clarify? Commented Jun 10, 2020 at 6:59

I read JSR 303: Bean Validation, beanvalidation.org/1.0/spec but still I do not get what validation will be performed on ScriptFile scriptFile .

Commented Jun 10, 2020 at 12:26

ScriptFile inside it probably has a bunch of fields and those fields have annotations on them as well. This is where the validation kicks in. Based on the original question it's not clear what exactly is inside ScriptFile class.

Commented Jun 10, 2020 at 13:06

Ok, thanks. Could you put an example of what could validate if its fields where a String, an Integer, and a Bean?

Commented Jun 10, 2020 at 13:44

IIRC @Valid isn't a Spring annotation but a JSR-303 annotation (which is the Bean Validation standard). What it does is it basically checks if the data that you send to the method is valid or not (it will validate the scriptFile for you).

answered Aug 29, 2010 at 14:56 Mateusz Dymczyk Mateusz Dymczyk 15.1k 10 10 gold badges 61 61 silver badges 94 94 bronze badges

What does it mean to "it will validate the scriptFile for you"? Check it is not null, it has some syntax, or some content? In other words, what does it validate and how? Should the user implement something? Where can I get info about it? Thank you!

Commented Jun 9, 2020 at 13:55

Please answer @nephewtom's question, it's not enough answer to validate with the missing main point that is "against what"?

Commented Nov 6, 2020 at 15:36

I think I know where your question is headed. And since this question is the one that pop ups in google's search main results, I can give a plain answer on what the @Valid annotation does.

I'll present 3 scenarios on how I've used @Valid

public class Employee < private String name; @NotNull(message="cannot be null") @Size(min=1, message="cannot be blank") private String lastName; //Getters and Setters for both fields. //. >

Controller for scenario 1:

 @RequestMapping("processForm") public String processFormData(@Valid @ModelAttribute("employee") Employee employee)

In this scenario, after submitting your form with an empty lastName field, you'll get an error page since you're applying validation rules but you're not handling it whatsoever.

Example of said error: Exception page

Controller for scenario 2:

 @RequestMapping("processForm") public String processFormData(@Valid @ModelAttribute("employee") Employee employee, BindingResult bindingResult)

In this scenario, you're passing all the results from that validation to the bindingResult, so it's up to you to decide what to do with the validation results of that form.

Controller for scenario 3:

@RequestMapping("processForm") public String processFormData(@Valid @ModelAttribute("employee") Employee employee) < return "employee-confirmation-page"; >@ExceptionHandler(MethodArgumentNotValidException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public Map invalidFormProcessor(MethodArgumentNotValidException ex) < //Your mapping of the errors. etc >

In this scenario you're still not handling the errors like in the first scenario, but you pass that to another method that will take care of the exception that @Valid triggers when processing the form model. Check this see what to do with the mapping and all that.

To sum up: @Valid on its own with do nothing more that trigger the validation of validation JSR 303 annotated fields (@NotNull, @Email, @Size, etc. ), you still need to specify a strategy of what to do with the results of said validation.

Hope I was able to clear something for people that might stumble with this.