1

I develop a Spring API and for any Exception or error case I want to send to a client a formal json answer.

I have already read aboud @ExceptionHandler and implemented it. But there is at least one case when the application throws an HTTP error without a corresponding Exception, hence I cannot generate a corresponding json answer for it.

When the API request is not supported (for example, I add an integer number to the path in the post request) the client receives:

<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>

and nothing in JSON format, which i little bit breakes the integrity of my application.

Is there something like an HTTP error handler for Spring? How can I generat a json answer for such a case?

Thank you in advance.

2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.