diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto.adoc index eb2e4be55d..501d810cef 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto.adoc @@ -582,29 +582,12 @@ This support depends on the chosen web server and the application environment, s [NOTE] ==== -Spring Boot does not support `h2c`, the cleartext version of the HTTP/2 protocol. -So you must <>. +Spring Boot does not advise using `h2c`, the cleartext version of the HTTP/2 protocol. +As a result, the next sections require to <>. +If you still choose to use `h2c`, you can check <>. ==== - -[[howto-configure-http2-undertow]] -==== HTTP/2 with Undertow -As of Undertow 1.4.0+, HTTP/2 is supported without any additional requirement on JDK8. - - - -[[howto-configure-http2-jetty]] -==== HTTP/2 with Jetty -For HTTP/2 support, Jetty requires the additional `org.eclipse.jetty.http2:http2-server` dependency. -Now depending on your deployment, you also need to choose other dependencies: - -* `org.eclipse.jetty:jetty-alpn-java-server` for applications running on JDK9+ -* `org.eclipse.jetty:jetty-alpn-openjdk8-server` for applications running on JDK8u252+ -* `org.eclipse.jetty:jetty-alpn-conscrypt-server` and the https://www.conscrypt.org/[Conscrypt library] with no JDK requirement - - - [[howto-configure-http2-tomcat]] ==== HTTP/2 with Tomcat Spring Boot ships by default with Tomcat 9.0.x which supports HTTP/2 out of the box when using JDK 9 or later. @@ -624,6 +607,16 @@ Starting Tomcat 9.0.x on JDK 8 without that native support logs the following er This error is not fatal, and the application still starts with HTTP/1.1 SSL support. +[[howto-configure-http2-jetty]] +==== HTTP/2 with Jetty +For HTTP/2 support, Jetty requires the additional `org.eclipse.jetty.http2:http2-server` dependency. +Now depending on your deployment, you also need to choose other dependencies: + +* `org.eclipse.jetty:jetty-alpn-java-server` for applications running on JDK9+ +* `org.eclipse.jetty:jetty-alpn-openjdk8-server` for applications running on JDK8u252+ +* `org.eclipse.jetty:jetty-alpn-conscrypt-server` and the https://www.conscrypt.org/[Conscrypt library] with no JDK requirement + + [[howto-configure-http2-netty]] ==== HTTP/2 with Reactor Netty @@ -637,6 +630,71 @@ Developers can choose to import only the required dependencies using a classifie +[[howto-configure-http2-undertow]] +==== HTTP/2 with Undertow +As of Undertow 1.4.0+, HTTP/2 is supported without any additional requirement on JDK8. + + + +[[howto-configure-http2-h2c]] +==== h2c with supported servers +To enable `h2c`, you need to leave the configprop:server.http2.enabled[] property set to `false`, +and instead apply a customizer specific to your choice of server: + +For Tomcat, we need to add an upgrade protocol: + +[source,java,indent=0,subs="verbatim,quotes,attributes"] +---- + @Bean + public TomcatConnectorCustomizer connectorCustomizer() { + return (connector) -> connector.addUpgradeProtocol(new Http2Protocol()); + } +---- + +For Jetty, we need to add a connection factory to the existing connector: + +[source,java,indent=0,subs="verbatim,quotes,attributes"] +---- + @Bean + public JettyServerCustomizer serverCustomizer() { + return (server) -> { + HttpConfiguration configuration = new HttpConfiguration(); + configuration.setSendServerVersion(false); + Arrays.stream(server.getConnectors()) + .filter(connector -> connector instanceof ServerConnector) + .map(ServerConnector.class::cast) + .forEach(connector -> { + connector.addConnectionFactory(new HTTP2CServerConnectionFactory(configuration)); + }); + }; + } +---- + +For Netty, we need to add `h2c` as a supported protocol: + +[source,java,indent=0,subs="verbatim,quotes,attributes"] +---- + @Bean + public NettyServerCustomizer serverCustomizer() { + return (server) -> server.protocol(HttpProtocol.H2C); + } +---- + +For Undertow, we need to enable the HTTP2 option: + +[source,java,indent=0,subs="verbatim,quotes,attributes"] +---- + @Bean + public UndertowBuilderCustomizer builderCustomizer() { + return (builder) -> { + builder.setServerOption(ENABLE_HTTP2, true); + }; + } +---- + + + + [[howto-configure-webserver]] === Configure the Web Server Generally, you should first consider using one of the many available configuration keys and customize your web server by adding new entries in your `application.properties` (or `application.yml`, or environment, etc. see "`<>`"). diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories index 4b7c83de5d..19c3c133e1 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories @@ -167,6 +167,7 @@ org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration,\ org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\ org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration,\ org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration,\ +org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration,\ org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration # AutoConfigureWebServiceClient diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTestAutoConfigurationIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTestAutoConfigurationIntegrationTests.java index 8561793111..6e39d92300 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTestAutoConfigurationIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTestAutoConfigurationIntegrationTests.java @@ -26,6 +26,7 @@ import org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAu import org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration; import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration; import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration; +import org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration; import org.springframework.context.ApplicationContext; import org.springframework.core.task.AsyncTaskExecutor; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; @@ -88,4 +89,9 @@ class WebMvcTestAutoConfigurationIntegrationTests { assertThat(this.applicationContext).has(importedAutoConfiguration(OAuth2ResourceServerAutoConfiguration.class)); } + @Test + void httpEncodingAutoConfigurationWasImported() { + assertThat(this.applicationContext).has(importedAutoConfiguration(HttpEncodingAutoConfiguration.class)); + } + }