How to get server ip address in Spring Boot Environment?
For port you want:
environment.getProperty("server.port");
And for the IP the server is listening on you want:
environment.getProperty("server.address");
As an aside, you can use @Value
to inject it directly into a String field without using Environment
like so:
@Value("${server.address}")
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;
@Component
public class MyListener implements ApplicationListener<EmbeddedServletContainerInitializedEvent> {
@Override
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.
INCORRECT SETUP
This failed previously with the firewall incorrectly setup
server.port=8081
server.address=192.168.0.93
Once the firewall is correctly setup you do not need to specify the server.address just the port.
CORRECT SETUP
server.port=8081
This allowed me to access the application correctly from other systems using its ip.
http://<someip>:<server.port>
http://192.168.0.93:8081
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
server.port=8090
For a random port use:
server.port=0
Similarly add application.yml
in /src/main/resources/
with:
server:
port: 8090
Related Topics
Regular Expression to Remove Everything But Characters and Numbers
Add Secure Flag to Jsessionid Cookie in Spring Automatically
Issue With Java 8 Collectors Type Mismatch: Cannot Convert from List<Object> to List<String>
Print Array Without Brackets and Commas
How to Move Marker Along Polyline Using Google Map
Using Streams to Convert a List of Objects into a String Obtained from the Tostring Method
How to Insert Null in MySQL Especially Int Datatype
How to Exclude Property from Lombok Builder
How to Check If a Date Is After Today Java
Eclipse Problems View Not Showing Errors Anymore
Calculating the Sum of Number and Its Reverse in Java
How to Modify Log4J Jsonlayout Field Names
How to Replace Every N-Th Character X from an String
Alternative to Switch Case in Java
How to Set a Description and an Example in Swagger With Swagger Annotations