Harmonize Discover the HTTP Port at Runtime How-To

See gh-22898
pull/23053/head
John Franco 4 years ago committed by Stephane Nicoll
parent cdbb7f441a
commit 303768b2b5

@ -498,7 +498,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<ServletWebServerInitializedEvent>` 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<ServletWebServerInitializedEvent>` (or `ApplicationListener<ReactiveWebServerInitializedEvent>` 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:

Loading…
Cancel
Save