15

Is there a way to customize what gets displayed when a required @RequestParam is not sent to the request handler? I always get HTTP Status 400 with a description "The request sent by the client was syntactically incorrect ()." in this case.

skaffman
  • 398,947
  • 96
  • 818
  • 769
rustyx
  • 80,671
  • 25
  • 200
  • 267

2 Answers2

17

Yes, there is a way you should catch MissingServletRequestParameterException

You can do it in several ways:

1)

   @ExceptionHandler(MissingServletRequestParameterException.class)
      public String handleMyException(Exception  exception) {
       return "yourErrorViewName";
              }  

2)

<error-page>
    <exception-type>org.springframework.web.bind.MissingServletRequestParameterException</exception-type>
    <location>/WEB-INF/pages/myError.jsp</location>
  </error-page>

Hope it helps.

danny.lesnik
  • 18,479
  • 29
  • 135
  • 200
3

How I solved my problem:

@ResponseBody
@ExceptionHandler(MissingServletRequestParameterException.class)
public Object missingParamterHandler(Exception exception) {
    // exception handle while specified arguments are not available requested service only. it handle when request is as api json service       
    return  new HashMap() {{ put("result", "failed"); put("type", "required_parameter_missing");}};
} 
Matthias
  • 7,432
  • 6
  • 55
  • 88
Janak Dhanani
  • 214
  • 4
  • 12