Annotations from Javax.Validation.Constraints Not Working

Annotations from javax.validation.constraints not working

For JSR-303 bean validation to work in Spring, you need several things:

  1. MVC namespace configuration for annotations: <mvc:annotation-driven />
  2. The JSR-303 spec JAR: validation-api-1.0.0.GA.jar (looks like you already have that)
  3. 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
  4. In the bean to be validated, validation annotations, either from the spec JAR or from the implementation JAR (which you have already done)
  5. In the handler you want to validate, annotate the object you want to validate with @Valid, and then include a BindingResult 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



Leave a reply



Submit