1

安らかな http API に spring mvc を使用しています。要求パラメータは、必要に応じて注釈を付けることができます

@RequestParam(value = "group", required = true) String someParam

必須パラメーターなしで API を呼び出すと、見苦しい情報が返されます。

HTTP/1.1 400 Bad Request
Content-Type: text/html;charset=ISO-8859-1
Cache-Control: must-revalidate,no-cache,no-store
Content-Length: 1380
Server: Jetty(8.1.9.v20130131)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Error 400 Bad Request</title>
</head>
<body><h2>HTTP ERROR 400</h2>
<p>Problem accessing /api/brokers. Reason:
<pre>Bad Request</pre></p><hr /><i><small>Powered by Jetty://</small></i><br/>
<br/>

</body>
</html>

この種のエラーをキャッチして、何が欠けているかを発信者に知らせることができます。

4

2 に答える 2

3

MissingServletRequestParameterExceptionをキャッチする必要があります

@ExceptionHandler(MissingServletRequestParameterException.class)
public String missingParamterHandler(Exception exception) {
  /*inspect and exception and obtain meaningful message*/
  return "default-error-view"; /*view name of your erro jsp*/
} 
于 2013-04-11T08:13:24.440 に答える
0
@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:43:23.077 に答える