From 9fc2f45af3afdb365256a737710ff8becaa64b53 Mon Sep 17 00:00:00 2001 From: John Franco Date: Tue, 11 Aug 2020 22:39:30 -0700 Subject: [PATCH] Add reactive web server port retrieval method The section about retrieving the bound port for an embedded web server had only the proper interface for servlet-based servers, not for reactive servers. It took me a while to figure it out, so adding what I found here might save other people the time that it cost me. --- .../spring-boot-docs/src/docs/asciidoc/howto.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 88b1bd0ae406..92a2b95bb65d 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 @@ -488,7 +488,7 @@ To scan for a free port (using OS natives to prevent clashes) use `server.port=0 [[howto-discover-the-http-port-at-runtime]] === Discover the HTTP Port at Runtime You can access the port the server is running on from log output or from the `ServletWebServerApplicationContext` through its `WebServer`. -The best way to get that and be sure that it has been initialized is to add a `@Bean` of type `ApplicationListener` and pull the container out of the event when it is published. +The best way to get that and be sure that it has been initialized is to add a `@Bean` of type `ApplicationListener` (or `ApplicationListener` for reactive servers) and pull the container out of the event when it is published. Tests that use `@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)` can also inject the actual port into a field by using the `@LocalServerPort` annotation, as shown in the following example: