Spring Boot REST @RequestParam not checked

I tried several examples from the network and cannot get Spring to check my query string parameter. It does not seem to execute 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 behavior

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

Desired behavior

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

thanks

+7
java spring spring-boot spring-mvc
source share
3 answers

You need to add @Validated to your class as follows:

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

UPDATE:

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

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

An example of exception handling:

 @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); } } 
+20
source share

You can try this

 @Pattern(regexp="^[0-9]+(,[0-9]+)*$") private static final String VALIDATION_REGEX; 

(note the final modifier) ​​or

  @Pattern() private static final String VALIDATION_REGEX = "^[0-9]+(,[0-9]+)*$"; 

And then remove @Pattern (regexp = VALIDATION_REGEX) from your method and save only the @Valid annotation:

 public myResonseObject getMyParams(@PathVariable("id") String id, @Valid @RequestParam(value = "myparam", required = true) String myParam) { 
0
source share

You have the wrong regular expression

 "^[0-9]+(,[0-9]+)*$" 

He will never analyze

 1,bob 

Perhaps you need:

 "^\w+(,\w+)*$" 

And if you need to parse also an empty string, use:

 "^(\w+(,\w+)*)?$" 
0
source share

All Articles