12

In Jersey 2 it is possible to do this:

@GET @PATH("user/{email}") public IDto getUser(@NotNull @Email @PathParam("email") String validEmail) { return userManagementService.findUserByEmail(validEmail); } 

But I cannot make something similar to work in Spring MVC, it seems that the validation is only done when providing an object in @RequestBody or using an SpringMVC Form, for example the following won't work:

@RequestMapping(value="/user/{email}", method = RequestMethod.GET) public @ResponseBody IDto getUser(@NotNull @Email @PathVariable String validEmail) { return userManagementService.findUserByEmail(validEmail); } 

There are other similar questions, but those seem to be oriented to Spring MVC UI applications, in my case it is only a REST API which returns JSON response so I don't have any View to map/bind to the controller.

2
  • You minimally need to annotate with @Valid, though this might only work if you have a model class and not just a String. Commented Jul 5, 2014 at 10:09
  • Yes, the question is about simple parameters not about POJO objects as input, for that I know about the @Valid annotation that will use the bean validation from the annotations in the class. But that is not the feature I am asking for Commented Jul 11, 2014 at 16:09

5 Answers 5

9

Seems it is possible, using @Validated.

Here's an example.

Based on OP's question, this should work:

@RestController @Validated public class MyController { @GetMapping(value="/user/{email}") public @ResponseBody IDto getUser(@NotNull @Email @PathVariable String validEmail) { return userManagementService.findUserByEmail(validEmail); } } 

In plain Spring implementations, it may be required to manually register the validator bean:

@Bean public MethodValidationPostProcessor methodValidationPostProcessor() { return new MethodValidationPostProcessor(); } 
Sign up to request clarification or add additional context in comments.

4 Comments

In addition to the link, can you please add a snippet so it is easier to understand the answer even if the link gets broken?
Adding @Validated to the required controller class or method is enough to trigger the validation.
In my case Is not working by default with @Validated annotation.
In plain Spring implementations, it may be required to register the validator bean.
5
  1. Simply add @Validated annotation at the top of your class.
  2. Put whatever annotations for validations (@NotBlank, @Min(1), etc.) before the @RequestParam annotation in your method signature.

3 Comments

"Put whatever annotations for validations ([…]) before the @RequestParam annotation in your method signature." — I just tested this, and it doesn't cause it to be validated. It would surprise me a great deal to learn that the framework cared the order in which these annotations were applied to the method parameter.
Actually, now that I reread it: were those supposed to be "do 1 or 2" or "do both"?
@M.Justin It's both.
3

The validated annotation from the org.springframework.validation.annotation.Validated package to validate a @PathVariable. Make sure the class annotated with @Validated.

@GetMapping("/name-for-day/{dayOfWeek}") public String getNameOfDay(@PathVariable("dayOfWeek") @Min(1) @Max(7) Integer dayOfWeek) { return dayOfWeek + ""; } 

Comments

2

Controller should be annotated with spring's @Validated

So update your code with

@Validated @RequestMapping(value="/user/{email}", method = RequestMethod.GET) public @ResponseBody IDto getUser( @NotNull @Email @PathVariable String validEmail) { return userManagementService.findUserByEmail(validEmail); } 

Comments

1

As far as I can tell, you cannot do this out-of-the-box with Spring.

Options:

  1. Use a regular expression:

    @RequestMapping(value="/user/{email:SOME_REXEXP}", method = RequestMethod.GET) public @ResponseBody IDto getUser(@PathVariable String validEmail) { return userManagementService.findUserByEmail(validEmail); } 
  2. Use Hibernate Validator to validate the method. Either call the validator manually, or make Spring call it for you using AOP. See https://github.com/gunnarmorling/methodvalidation-integration

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.