Tomcat - how to get http 500 instead of 404 when the application was not deployed correctly?

We have several REST applications using Spring MVC. From time to time, some application after deployment does not start. When a Javascript client tries to access the resource URL, it receives a 404 status code. Therefore, it assumes that the resource does not exist. The http status 500 returned by Tomcat will be more suitable for us. Is it possible to change this default behavior of Tomcat?

I found a similar problem with JBoss (uses the built-in Tomcat), but did not answer: https://serverfault.com/questions/367986/mod-jk-fails-to-detect-error-state-because-jboss-gives-404- not-500

+3
source share
2 answers

HTTP proxy

If you have some kind of proxy in front of your Tomcat server (e.g. apache or), I believe that it can be configured to translate 404 into another status code and an error page. If you do not have a proxy server or want the solution to remain self-sufficient:

Custom Spring Loader and Servlet Filter

Since you are using Spring, I assume that you downloaded it using ContextLoaderListenerin web.xml:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

This class is responsible for loading Spring, and this is a step that in most cases causes the application to fail to start. Just extend this class and swallow any exception so that it never reaches the servlet container, so Tomcat will not think that your application deployment failed:

public class FailSafeLoaderListener extends ContextLoaderListener {

    private static final Logger log = LoggerFactory.getLogger(FailSafeLoaderListener.class);

    @Override
    public void contextInitialized(ServletContextEvent event) {
        try {
            super.contextInitialized(event);
        } catch (Exception e) {
            log.error("", e);
            event.getServletContext().setAttribute("deployException", e);
        }
    }
}

- Spring , ServletContext. web.xml:

<listener>
    <listener-class>com.blogspot.nurkiewicz.download.FailSafeLoaderListener</listener-class>
</listener>

, Spring:

public class FailSafeFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Override
    public void destroy() {}

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        Exception deployException = (Exception) request.getServletContext().getAttribute("deployException");
        if (deployException == null) {
            chain.doFilter(request, response);
        } else {
            ((HttpServletResponse) response).sendError(500, deployException.toString());
        }
    }
}

(, , ?):

<filter-mapping>
    <filter-name>failSafeFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

, , .

0

, .

:

  • , - :

    if (req.getContextPath().isEmpty()){
        resp.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
    } else {
        resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
    }
    
  • jar, tomcat lib.

  • conf/web.xml, *.404

  • 404 /404.404.

    <error-page>
        <error-code>404</error-code>
        <location>/404.404</location>
    </error-page>
    

, .

0

All Articles