我开发了一个Spring API,对于任何异常或错误情况,我想向客户端发送正式的json答案。
我已经阅读了大量@ExceptionHandler
并实施了它。但是至少有一种情况是应用程序在没有相应的异常的情况下抛出HTTP错误,因此我无法为它生成相应的json答案。
当不支持API请求时(例如,我在post请求中的路径中添加一个整数),客户端会收到:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
<title>Error 405</title>
</head>
<body>
<h2>HTTP ERROR: 405</h2>
<p>
Problem accessing /a/wrong/path/67. Reason:
<pre>Request method 'POST' not supported</pre>
</p>
<hr/>
<i>
<small>Powered by Jetty://</small>
</i>
</body>
</html>
并且没有任何JSON格式,这有点破坏了我的应用程序的完整性。
是否有类似Spring的HTTP错误处理程序?如何为这种情况生成json答案?
提前谢谢。