Reputation: 85
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,
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!!
Upvotes: 3
Views: 6789
Reputation: 2643
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");
}
});
};
}
}
Upvotes: 0