11

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.
    – beerbajay
    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
    – raspacorp
    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();
}
4
  • 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?
    – raspacorp
    Jun 26, 2017 at 19:15
  • 1
    Adding @Validated to the required controller class or method is enough to trigger the validation.
    – Dormouse
    Jun 27, 2017 at 9:07
  • In my case Is not working by default with @Validated annotation. Sep 14, 2017 at 15:38
  • In plain Spring implementations, it may be required to register the validator bean.
    – Dormouse
    Aug 23, 2022 at 10:09
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
  • "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.
    – M. Justin
    Mar 27 at 19:33
  • Actually, now that I reread it: were those supposed to be "do 1 or 2" or "do both"?
    – M. Justin
    Mar 27 at 20:43
  • 1
    @M.Justin It's both.
    – Hani
    Mar 31 at 4:52
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);
}
2

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 + "";
}
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.