Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javax bean validation not working on method parameters

javax validation not working on method parameters.. This is a test code and none of javax validation works on method parameter...

@RequestMapping(value = "/{id}", method = RequestMethod.PUT, params = "action=testAction")
public Test update(
        @Size(min = 1) @RequestBody List<String> ids,
        @Min(3) @PathVariable String name) {
    return doSomething(ids, name);
}

But i have class level validations which works perfectly...

@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public RoleType create (@RequestBody @Validated(FieldType.class) User user) {
    ...
}

And

@Size(min = 2, max = 10, groups = { FieldType.class }, message = "Invalid user code")
 public String getId() {
    return _id  ;
}

-- Solution --

all steps followed as per the accepted answer. And another addition is annoation on class level

@Validated
class UserController
{
   @RequestMapping(value = "/{id}", method = RequestMethod.PUT, params ="action=testAction")
   public Test update(@Size(min = 1) @RequestBody List<String> ids,@Min(3) @PathVariable String name) {
    return doSomething(ids, name);
}
}
like image 434
surya Avatar asked Oct 20 '16 18:10

surya


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.

How do you validate query parameters in spring boot?

Validating a PathVariable Just as with @RequestParam, we can use any annotation from the javax. validation. constraints package to validate a @PathVariable. The default message can be easily overwritten by setting the message parameter in the @Size annotation.

What does @validated do?

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

you need to register MethodValidationPostProcessor bean to kick method level validation annotation

delegates to a JSR-303 provider for performing method-level validation on annotated methods.

  @Bean
     public MethodValidationPostProcessor methodValidationPostProcessor() {
          return new MethodValidationPostProcessor();
     }

then,

@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public Test update(
        @Size(min = 1) @RequestBody List<String> ids,
        @Min(3) @PathVariable("id") String name) {
    return doSomething(ids, name);
}

if you want to handle validation exception

@ExceptionHandler(value = { ConstraintViolationException.class })
 @ResponseStatus(value = HttpStatus.BAD_REQUEST)
 public String handleResourceNotFoundException(ConstraintViolationException e) {
      Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
      StringBuilder strBuilder = new StringBuilder();
      for (ConstraintViolation<?> violation : violations ) {
           strBuilder.append(violation.getMessage() + "\n");
      }
      return strBuilder.toString();
 }
like image 188
kuhajeyan Avatar answered Sep 28 '22 05:09

kuhajeyan