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.
Validating a PathVariable Let's consider an example where we validate that a String parameter isn't blank and has a length of less than or equal to 10: @GetMapping("/valid-name/{name}") public void createUsername(@PathVariable("name") @NotBlank @Size(max = 10) String username) { // ... }
In Spring MVC, the @RequestParam annotation is used to read the form data and bind it automatically to the parameter present in the provided method. So, it ignores the requirement of HttpServletRequest object to read the provided data.
@RequestParam is a Spring annotation used to bind a web request parameter to a method parameter. It has the following optional elements: defaultValue - used as a fallback when the request parameter is not provided or has an empty value. name - name of the request parameter to bind to.
The Spring MVC Validation is used to restrict the input provided by the user. To validate the user's input, the Spring 4 or higher version supports and use Bean Validation API. It can validate both server-side as well as client-side applications.
In this quick tutorial, we’ll explore Spring's @RequestParam annotation and its attributes. Simply put, we can use @RequestParam to extract query parameters, form parameters, and even files from the request. In this article, we introduce different types of @RequestMapping shortcuts for quick web development using traditional Spring MVC framework.
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.
@Validated - To perform validation on each method of controller if any. @RequestParam - To accept web request parameter in variable. ( Note: All variable annotated with @RequestParam are compulsory/mandatory for request, until you set required = false @RequestParam (required = false) for that parameter)
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.
Seems it is possible, using @Validated
.
Here's an example.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With