From 4c8626ea56be87932656a5a46b6cd6218521607f Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 16 Jan 2020 09:33:22 +0000 Subject: [PATCH] Allow Boot's Jetty error handler to be overridden Fixes gh-19520 --- .../jetty/JettyServletWebServerFactory.java | 4 ++-- .../JettyServletWebServerFactoryTests.java | 24 ++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyServletWebServerFactory.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyServletWebServerFactory.java index 63445ec0d2..62adc85fff 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyServletWebServerFactory.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyServletWebServerFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -327,9 +327,9 @@ public class JettyServletWebServerFactory extends AbstractServletWebServerFactor ServletContextInitializer... initializers) { List configurations = new ArrayList<>(); configurations.add(getServletContextInitializerConfiguration(webAppContext, initializers)); - configurations.addAll(getConfigurations()); configurations.add(getErrorPageConfiguration()); configurations.add(getMimeTypeConfiguration()); + configurations.addAll(getConfigurations()); return configurations.toArray(new Configuration[0]); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/jetty/JettyServletWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/jetty/JettyServletWebServerFactoryTests.java index 26708b4a9a..ea794ec061 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/jetty/JettyServletWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/jetty/JettyServletWebServerFactoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,8 +35,10 @@ import org.eclipse.jetty.server.SslConnectionFactory; import org.eclipse.jetty.server.handler.ErrorHandler; import org.eclipse.jetty.server.handler.HandlerCollection; import org.eclipse.jetty.server.handler.HandlerWrapper; +import org.eclipse.jetty.servlet.ErrorPageErrorHandler; import org.eclipse.jetty.util.thread.QueuedThreadPool; import org.eclipse.jetty.util.thread.ThreadPool; +import org.eclipse.jetty.webapp.AbstractConfiguration; import org.eclipse.jetty.webapp.Configuration; import org.eclipse.jetty.webapp.WebAppContext; import org.junit.jupiter.api.Test; @@ -301,4 +303,24 @@ class JettyServletWebServerFactoryTests extends AbstractJettyServletWebServerFac }); } + @Test + void errorHandlerCanBeOverridden() { + JettyServletWebServerFactory factory = getFactory(); + factory.addConfigurations(new AbstractConfiguration() { + + @Override + public void configure(WebAppContext context) throws Exception { + context.setErrorHandler(new CustomErrorHandler()); + } + + }); + JettyWebServer jettyWebServer = (JettyWebServer) factory.getWebServer(); + WebAppContext context = (WebAppContext) jettyWebServer.getServer().getHandler(); + assertThat(context.getErrorHandler()).isInstanceOf(CustomErrorHandler.class); + } + + private static class CustomErrorHandler extends ErrorPageErrorHandler { + + } + }