@valid annotation is not working as expected
Your @Valid
annotation is applied to the List and not the actual objects present on the list.
The annotation you have in your example validates the list and not each object of the list.
Try this
public Student addStudent(@PathVariable Integer classId,@RequestBody List<@Valid Student> students)
You can specify additional parameters as well for example you may need to validate each object on the list AND to make sure that the list is not empty, or has a minimum size etc. Those validations must be performed on the list.
How to test if @Valid annotation is working?
This solution works with Spring 5. It should work with Spring 4 as well. (I've tested it on Spring 5 and SpringBoot 2.0.0).
There are three things that have to be there:
- in the test class, provide a bean for method validation (PostServiceTest in your example)
Like this:
@TestConfiguration
static class TestContextConfiguration {
@Bean
public MethodValidationPostProcessor bean() {
return new MethodValidationPostProcessor();
}
}
- in the class that has @Valid annotations on method, you also need to annotate it with @Validated (org.springframework.validation.annotation.Validated) on the class level!
Like this:
@Validated
class PostService {
public Post save(@Valid Post post) {
return postRepository.save(post);
}
}
- You have to have a Bean Validation 1.1 provider (such as Hibernate Validator 5.x) in the classpath. The actual provider will be autodetected by Spring and automatically adapted.
More details in MethodValidationPostProcessor documentation
Hope that helps
Annotations from javax.validation.constraints not working
For JSR-303 bean validation to work in Spring, you need several things:
- MVC namespace configuration for annotations:
<mvc:annotation-driven />
- The JSR-303 spec JAR:
validation-api-1.0.0.GA.jar
(looks like you already have that) - An implementation of the spec, such as Hibernate Validation, which appears to be the most commonly used example:
hibernate-validator-4.1.0.Final.jar
- In the bean to be validated, validation annotations, either from the spec JAR or from the implementation JAR (which you have already done)
- In the handler you want to validate, annotate the object you want to validate with
@Valid
, and then include aBindingResult
in the method signature to capture errors.
Example:
@RequestMapping("handler.do")
public String myHandler(@Valid @ModelAttribute("form") SomeFormBean myForm, BindingResult result, Model model) {
if(result.hasErrors()) {
...your error handling...
} else {
...your non-error handling....
}
}
Related Topics
Handling Static Variables in Multithreaded Java Program
Checking If a String Contains a Dot
Aws Lambda: How to Extract a Tgz File in a S3 Bucket and Put It in Another S3 Bucket
Automatically Convert Style Sheets to Inline Style
How to Check If an Arraylist of Strings Contains Substrings of Another Arraylist of Strings
Value Annotation Not Working in Junit Test
Checking If a String Is Contained in an Enum Set in O(1)
Java Streams Sum Values of a List of Maps
Statefulbeantocsv With Column Headers
Converting Number Representing a Date in Excel to a Java Date Object
Escaping New Line Character Within SQL Query
How to Make a Java Program Quit When "Q" Is Inputted, Issue Is the Default Input Variable Is Double
How to Retrieve Element Value from Soap Response Using Java
Delete Not Working in Hibernate/Spring Data
Spring Boot @Enablescheduling Conditionally
Limiting the Number of Characters in a String, and Chopping Off the Rest
Disable Spring Security Config Class for @Webmvctest in Spring Boot
Maven 3.8.0 Compiler - Fatal Error Compiling: Release Version 11 Not Supported