Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a server port at runtime in Spring Webflux?

I have a Spring Webflux application with Spring Boot ver 2.3.5

How can I get a server port of running Netty container at runtime? (Not in tests)

Note: nor @Value("${server.port}") neither @Value("${local.server.port}") do not work if the port is not specified in the configuration.

like image 704
fyrkov Avatar asked Oct 14 '25 13:10

fyrkov


1 Answers

After I looking into the spring boot library, I found that there is a ReactiveWebServerApplicationContext. It can give you the port.

@Autowired
private ReactiveWebServerApplicationContext server;

@GetMapping
public Flux<YourObject> getSomething() {
    var port = server.getWebServer().getPort());
    ...
}
like image 73
zlaval Avatar answered Oct 17 '25 04:10

zlaval



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!