Skip to content

Since 2.2.2 Jetty error handler cannot be overridden #19520

@oleborup

Description

@oleborup

We use Spring Boot with Jetty and Jersey for microservices. We want to expose as little server information as possible so the Jetty server is customized with silent error handler to return no content on errors.

/**
 * Custom Jetty container with no server tag and content empty error response
 */
@Bean
public ServletWebServerFactory servletContainer() {
    JettyServletWebServerFactory factory = new JettyServletWebServerFactory();
    factory.setDocumentRoot(new File(System.getProperty("java.io.tmpdir")));
    factory.addServerCustomizers(server -> {
        Handler handler = server.getHandler();
        StatisticsHandler stats = new StatisticsHandler();
        stats.setHandler(handler);
        server.setHandler(stats);
        if (handler instanceof GzipHandler) {
            handler = ((GzipHandler) handler).getHandler();
        }
        WebAppContext context = (WebAppContext) handler;
        context.setErrorHandler(new SilentErrorHandler());
        for (Connector connector : server.getConnectors()) {
            for (ConnectionFactory connFac : connector.getConnectionFactories()) {
                if (connFac instanceof HttpConnectionFactory) {
                    ((HttpConnectionFactory) connFac).getHttpConfiguration().setSendServerVersion(false);
                }
            }
        }
    });
    return factory;
}

After upgrade from 2.2.1 to 2.2.2 the custom error handler is no longer invoked.

A minimal sample can be found here: https://github.com/oleborup/spring-boot-jetty-jersey-error-handling

Tests will fail when changing version from 2.2.1 to 2.2.2.

In some cases we use MVC for static resources and property(ServletProperties.FILTER_STATIC_CONTENT_REGEX, "(/.*\\.html)"); in JerseyConfig. For errors in the static matched cases the default MVC error handler started to be invoked after upgrade to 2.2.2. Solved with @EnableAutoConfiguration(exclude = {ErrorMvcAutoConfiguration.class}) but again the default Jetty error handler was invoked by JettyEmbeddedErrorHandler and not the custom error handler.

Best regards, Ole

Metadata

Metadata

Assignees

Labels

type: regressionA regression from a previous release

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions