java - How to handle jersey and tomcat errors consistently? -
I have applied with some (REST) service jersey if a wrong request jersey handles errors and answers with some JSON content is. There is an exception mapper that should catch everyone:
public class MyExceptionMapper exceptionsManper < Throwable & gt;
But if there is an invalid HTTP request - e.g. Invalid Content Type - Jersey is an opportunity to do this before Tomcat handles exceptions. The resulting response is a somewhat ugly badge html error page instead of the desired JSON
I know that it is possible to set a . & Lt; Error page & gt;
In the deployment descriptor but there I have no access to any error details.
Is there a way to prevent this from catching this error? If so, the jersey can hold it with its ExceptionMapper and return the correct answer.
You know that you can set up an "error page", but what do you mean by this Are you? If this is just a static webpage, yeah, you will not have access to the error details. But if you forward it to a servlet, which is done to handle the errors, then you should be aware of your own error, and can return jerseys back to you.
i.e.
web.xml:
& lt; Error-page & gt; & Lt; Error code & gt; 415 & lt; / Error code & gt; & Lt; Location & gt; / InvalidContentHandler & lt; / Location & gt; & Lt; / Error page & gt; & Lt; Error page & gt; & Lt; Exception Type & gt; Java.lang.Throwable & lt; / Exception type & gt; & Lt; Location & gt; / InvalidContentHandler & lt; / Location & gt; & Lt; / Error page & gt;
Note: In web.xml above, you get the actual exception type you are facing, which you can get with java.lang .Throwable should change the "javax.servlet.error.exception" attribute, shown below
InvalidContentHandler.java:.
Protects zero doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {processError (request, response); } Protected Zero doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {processError (request, response); } Private Zero processError (HttpServletRequest request, HttpServletResponse response) IOException {throws // jersey to increase control, or get some information: throwable thrownable = (throwable) request.getAttribute ("javax.servlet.error.exception "); Integer status code = (integer) request .get attribute ("javax.servlet.error.status_code"); String servletname = (string) request.getAttribute ("javax.servlet.error.servlet_name"); String request URI = (string) request.getAttribute ("javax.servlet.error.request_uri"); ...}
Comments
Post a Comment