3

I'm developping a java application (jar file) using spring boot (2.1.3.RELEASE) with embedded tomcat (version 9.0.16).

I want to override the Catalina ErrorReportValve error page of tomcat,

ErrorReportValve default error page

In order to do that, i need to point Tomcat to use the custom class that i created for this purpose instead of the default class. This can be done by updating the Host element in Tomcat's configuration file 'server.xml'.

custom class:

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.logging.Logger;

import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.catalina.valves.ErrorReportValve;

public class CustomErrorReportValve extends ErrorReportValve {

    // Create a simple logger
    Logger log = Logger.getLogger(CustomErrorReportValve.class.getName());

    @Override
    protected void report(Request request, Response response, Throwable t) {
        try {
            // Write a more friendly, less technical message to the user
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(response.getOutputStream()));
            out.write("<html><head><title>Oops</title><body>");
            out.write("<h1>Oops</h1>");
            out.write("<p>Well, that didn't go as we had expected.</p>");
            out.write("<p>Don't worry though, we're working on it.</p>");
            out.write("</body></html>");
            out.close();

            // Log the error with your favorite logging framework...
            log.severe("Uncaught throwable was thrown: " + t.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

server.xml (or tomcat-server.xml when using embedded tomcat):

    <Valve className="org.apache.catalina.valves.ErrorReportValve" showReport="false" showServerInfo="false" />

I found in the spring docs, that when using embedded tomcat in spring the configuration file is called tomcat-server.xml rather than server.xml.

Now, the problem is that i can't find the tomcat-server.xml in a spring boot project, or how to configure/override it, if not, is there any other way to customize the catalina ErrorReportValve error page in springboot with embedded tomcat?

Thanks for the help!!

Elia Kallas
  • 45
  • 2
  • 8
  • No you don't. Use java to configure it by registering a `TomcatCustomizer` to do what yu want. – M. Deinum Nov 19 '20 at 18:19
  • Thanks @M. Deinum, that's exactly what I was searching for, i found this article that explain the problem and how to customize these errors, link : https://www.gitmemory.com/issue/spring-projects/spring-boot/21257/636275409 – Elia Kallas Nov 21 '20 at 15:39
  • Link is dead. Any other reference? – lubrum Dec 02 '22 at 15:09

1 Answers1

0

It seems your question relates following issue:

import org.apache.catalina.Container;
import org.apache.catalina.core.StandardHost;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyConfig {
    // https://docs.spring.io/spring-boot/docs/2.4.4/reference/htmlsingle/#howto-use-tomcat-legacycookieprocessor
    // https://github.com/spring-projects/spring-boot/issues/21257#issuecomment-745565376
    @Bean
    public WebServerFactoryCustomizer<TomcatServletWebServerFactory> errorReportValveCustomizer() {

        return (factory) -> {
            factory.addContextCustomizers(context -> {
                final Container parent = context.getParent();
                if (parent instanceof StandardHost) {
                    ((StandardHost) parent).setErrorReportValveClass(
                        "your.package.CustomErrorReportValve");
                }
            });
        };
    }
}