Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to validate @RequestParam in Spring MVC REST endpoint?

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.

like image 296
raspacorp Avatar asked Jul 05 '14 03:07

raspacorp


People also ask

How do you validate a PATH variable?

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) { // ... }

What is the use of @RequestParam in Spring MVC?

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.

What is @RequestParam in REST API?

@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.

Does Spring MVC support data validation?

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.

What is @requestparam in Spring MVC?

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.

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 the difference between @validated and @requestparam in Salesforce?

@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)

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.


Video Answer


2 Answers

Seems it is possible, using @Validated.

Here's an example.

like image 179
Dormouse Avatar answered Oct 10 '22 19:10

Dormouse


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.

like image 32
Hani Avatar answered Oct 10 '22 21:10

Hani