Spring Boot - How to Get Running Port and Ip Address

How to get server ip address in Spring Boot Environment?

For port you want:


And for the IP the server is listening on you want:


As an aside, you can use @Value to inject it directly into a String field without using Environment like so:

private String serverAddress;

Spring Boot - How to get the running port

Thanks to @Dirk Lachowski for pointing me in the right direction. The solution isn't as elegant as I would have liked, but I got it working. Reading the spring docs, I can listen on the EmbeddedServletContainerInitializedEvent and get the port once the server is up and running. Here's what it looks like -

import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

public class MyListener implements ApplicationListener<EmbeddedServletContainerInitializedEvent> {

public void onApplicationEvent(final EmbeddedServletContainerInitializedEvent event) {
int thePort = event.getEmbeddedServletContainer().getPort();

How to find port number of a Spring Boot app?

localhost:<your port> may not respond at all. Make sure you're hitting an endpoint you know is availible like localhost:8080/info. Make sure your app has completly started, it could take serveral minutes.

As @BrianC mentioned make sure your app is actually a web server.

How to setup spring-boot to allow access to the webserver from outside IP addresses

Since so many people have viewed this question. The resolution was to make sure the firewall was configured correctly on the hosting CentOS machine and not set the server address explicitly.


This failed previously with the firewall incorrectly setup


Once the firewall is correctly setup you do not need to specify the server.address just the port.



This allowed me to access the application correctly from other systems using its ip.


How to configure port for a Spring Boot application

As said in docs either set server.port as system property using command line option to jvm -Dserver.port=8090 or add application.properties in /src/main/resources/ with


For a random port use:


Similarly add application.yml in /src/main/resources/ with:

port: 8090

Related Topics

Leave a reply