HTTP proxy
If you have some kind of proxy in front of your Tomcat server (e.g. apache ornginx), 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>
, , .