Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot REST @RequestParam not being Validated

Tags:

I have tried a number of examples from the net and cannot get Spring to validate my query string parameter. It doesn't seem execute the REGEX / fail.

package my.controller;  import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;  import javax.validation.Valid; import javax.validation.constraints.Pattern;  import static org.springframework.web.bind.annotation.RequestMethod.GET;  @RestController public class MyController {      private static final String VALIDATION_REGEX = "^[0-9]+(,[0-9]+)*$";      @RequestMapping(value = "/my/{id}", method = GET)     public myResonseObject getMyParams(@PathVariable("id") String id,                                        @Valid @Pattern(regexp = VALIDATION_REGEX)                                         @RequestParam(value = "myparam", required = true) String myParam) {          // Do Stuff!     }  } 

Current behaviour

PASS - /my/1?myparam=1 PASS - /my/1?myparam=1,2,3 PASS - /my/1?myparam= PASS - /my/1?myparam=1,bob 

Desired behaviour

PASS - /my/1?myparam=1 PASS - /my/1?myparam=1,2,3 FAIL - /my/1?myparam= FAIL - /my/1?myparam=1,bob 

Thanks

like image 1000
ptimson Avatar asked Jul 27 '16 13:07

ptimson


People also ask

What is @requestparam annotation in Spring Boot?

Hey guys in this post we will discuss Spring boot @RequestParam annotation and its example The @RequestParam annotation binds the web request parameter to a controller method. In other words, @RequestParam annotation is used to obtain a parameter from the URI

What are the new annotations in REST API in Spring Boot?

As we already discussed few annotations in Creating first rest api in spring-boot so we will talk about new annotations only. @Validated - To perform validation on each method of controller if any. @RequestParam - To accept web request parameter in variable.

How to capture parameters in spring rest request?

In Spring REST, parameters in request URI are captured via @PathVariable and all query parameters via @RequestParam. Please note that maven dependency shall be added and ConstraintViolationException should be handled as described above. 2.1.

What is @pathvariable in spring rest?

Query and path parameter validation In Spring REST, parameters in request URI are captured via @PathVariable and all query parameters via @RequestParam. Please note that maven dependency shall be added and ConstraintViolationException should be handled as described above.


1 Answers

You need add @Validated to your class like this:

@RestController @Validated class Controller {   // ... } 

UPDATE:

you need to configure it properly.. add this bean to your context:

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

Example to handle exception:

@ControllerAdvice @Component public class GlobalExceptionHandler {     @ExceptionHandler     @ResponseBody     @ResponseStatus(HttpStatus.BAD_REQUEST)     public Map handle(MethodArgumentNotValidException exception) {         return error(exception.getBindingResult().getFieldErrors()                 .stream()                 .map(FieldError::getDefaultMessage)                 .collect(Collectors.toList()));     }       @ExceptionHandler     @ResponseBody     @ResponseStatus(HttpStatus.BAD_REQUEST)     public Map handle(ConstraintViolationException exception) {         return error(exception.getConstraintViolations()                 .stream()                 .map(ConstraintViolation::getMessage)                 .collect(Collectors.toList()));     }      private Map error(Object message) {         return Collections.singletonMap("error", message);     } } 
like image 85
Jaiwo99 Avatar answered Oct 05 '22 07:10

Jaiwo99