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....
}
}
Annotations from javax.validation.constraints not working (ignored)
From @JBNizet comment
Replace @Min(value= 5)
(and the other validation annotations too, of course) by @field:Min(value= 5)
.
validation annotation constraint not working
There is a problem with the field sales
- you cannot use @Size
with number types. According to the docs:
Supported types are:
CharSequence (length of character sequence is evaluated)
Collection (collection size is evaluated)
Map (map size is evaluated)
Array (array length is evaluated)
In your case (with Float) you need to use a different set of annotations:
@NotNull
@DecimalMax("100000")
@DecimalMin("10")
private Float sales;
Notice that I also placed @NotNull
because @DecimalMin
and @DecimalMax
consider nullable elements as valid (it's the common approach of Bean Validation API).
All annotations are from the package javax.validation.constraints
.
javax.validation.constraints does not work with Spring web
you need to add @Validated
annotation on your controller, which marks it to be validated.
@Slf4j
@Validated
@RestController
@RequestMapping("/auth-api")
public class AuthController {
// your code
}
Additionally, you have to make sure you have one validator implementation like hibernate-validator
in your classpath.
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.10.Final</version>
</dependency>
If not already present, you can add above dependency in your pom.xml or any other suitable version from here
My custom validation annotation is not working in Spring Boot
Record classes are in fact auto-generated classes with immutable fields and a getter-style method for each field. How do you know where (on a method or a field) is the annotation @SpecificStringValue
placed?
The target of the annotation is only PARAMETER
so i suspect that the annotation is not propagated to the field (userName), which is what will trigger the validation. You should at least try to add METHOD
and FIELD
to the annotation targets
Related Topics
How to Decode a String Encoded with Openssl Aes-128-Cbc Using Java
How to Determine If a Point Is Inside a 2D Convex Polygon
How to Implement a Fsm - Finite State MAChine in Java
Create Whole Path Automatically When Writing to a New File
How to Get Java Logging Output to Appear on a Single Line
Java Code for Getting Current Time
How to Add a Timeout Value When Using Java's Runtime.Exec()
Java Threads and Number of Cores
How to Improve the Performance of G.Drawimage() Method for Resizing Images
Read Error Response Body in Java
Differencebetween Canonical Name, Simple Name and Class Name in Java Class
JPA - Returning an Auto Generated Id After Persist()
How to Analyze Information from a Java Core Dump
How an Object Will Call Tostring Method Implicitly
The Most Sophisticated Way for Creating Comma-Separated Strings from a Collection/Array/List