Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Spring - how to handle missing required request parameters

Consider the following mapping:

@RequestMapping(value = "/superDuperPage", method = RequestMethod.GET) public String superDuperPage(@RequestParam(value = "someParameter", required = true) String parameter) {     return "somePage"; } 

I want to handle the missing parameter case by not adding in required = false. By default, 400 error is returned, but I want to return, let's say, a different page. How can I achieve this?

like image 433
peech Avatar asked Jun 10 '16 10:06

peech


People also ask

How does Spring boot handle optional request parameters?

You can do it in three ways: Set required = false in @RequestParam annotation. Set defaultValue = “any default value” in @RequestParam annotation. Using Optional keyword.

How do I pass a parameter in Spring boot request?

Spring Boot made passing parameters easy with Java annotations. In the above URL, there are two parameters which are v and t. To pass the parameters, put “?”. Then, add the parameter name followed by “=” and the value of the parameter.

Is request Param mandatory?

By default, all the request parameters annotated with @RequestParam are mandatory. Thus in the previous examples, if we skip a query string parameter from a request, we get a Bad Request response. ~ curl -i 'http://localhost:8080/data4' -- HTTP/1.1 400 ...

Can we use both PathVariable and RequestParam?

The @PathVariable annotation is used for data passed in the URI (e.g. RESTful web services) while @RequestParam is used to extract the data found in query parameters. These annotations can be mixed together inside the same controller. @PathParam is a JAX-RS annotation that is equivalent to @PathVariable in Spring.


2 Answers

If a required @RequestParam is not present in the request, Spring will throw a MissingServletRequestParameterException exception. You can define an @ExceptionHandler in the same controller or in a @ControllerAdvice to handle that exception:

@ExceptionHandler(MissingServletRequestParameterException.class) public void handleMissingParams(MissingServletRequestParameterException ex) {     String name = ex.getParameterName();     System.out.println(name + " parameter is missing");     // Actual exception handling } 

I want to return let's say a different page. How to I achieve this?

As the Spring documentation states:

Much like standard controller methods annotated with a @RequestMapping annotation, the method arguments and return values of @ExceptionHandler methods can be flexible. For example, the HttpServletRequest can be accessed in Servlet environments and the PortletRequest in Portlet environments. The return type can be a String, which is interpreted as a view name, a ModelAndView object, a ResponseEntity, or you can also add the @ResponseBody to have the method return value converted with message converters and written to the response stream.

like image 183
Ali Dehghani Avatar answered Sep 16 '22 17:09

Ali Dehghani


An alternative

If you use the @ControllerAdvice on your class and if it extends the Spring base class ResponseEntityExceptionHandler. A pre-defined function has been created on the base class for this purpose. You have to override it in your handler.

    @Override protected ResponseEntity<Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {     String name = ex.getParameterName();     logger.error(name + " parameter is missing");      return super.handleMissingServletRequestParameter(ex, headers, status, request); } 

This base class is very useful, especially if you want to process the validation errors that the framework creates.

like image 43
Eric Giguere Avatar answered Sep 20 '22 17:09

Eric Giguere