15

@RequestParam必要なものがリクエストハンドラに送信されないときに表示されるものをカスタマイズする方法はありますか?この場合、「クライアントから送信されたリクエストは構文的に正しくありませんでした()」という説明が付いたHTTPステータス400が常に表示されます。

4

2 に答える 2

18

はい、キャッチする方法がありますMissingServletRequestParameterException

いくつかの方法でそれを行うことができます:

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>

それが役に立てば幸い。

于 2012-01-28T14:25:31.703 に答える
5

私の問題をどのように解決したか:

@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");}};
} 
于 2014-09-19T05:45:29.287 に答える