java—在spring boot中在嵌入式ApacheTomcat中配置“tomcat server.xml”文件

oknrviil  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(499)

我正在使用SpringBoot(2.1.3.release)和嵌入式tomcat(9.0.16版)开发一个java应用程序(jar文件)。
我想覆盖tomcat的catalina errorreportvalve错误页,

为此,我需要指示tomcat使用为此目的创建的自定义类,而不是默认类。这可以通过更新tomcat的配置文件“server.xml”中的host元素来实现。
自定义类:

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(或使用嵌入式tomcat时的tomcat-server.xml):

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

我在spring文档中发现,在spring中使用嵌入式tomcat时,配置文件名为tomcat-server.xml,而不是server.xml。
现在,问题是我在springboot项目中找不到tomcat-server.xml,或者如何配置/覆盖它,如果没有,是否有其他方法可以使用嵌入的tomcat在springboot中自定义catalina errorreportvalve错误页?
谢谢你的帮助!!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题