Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get local server host and port in Spring Boot?

I'm starting up a Spring Boot application with mvn spring-boot:run.

One of my @Controllers needs information about the host and port the application is listening on, i.e. localhost:8080 (or 127.x.y.z:8080). Following the Spring Boot documentation, I use the server.address and server.port properties:

@Controller public class MyController {      @Value("${server.address}")     private String serverAddress;      @Value("${server.port}")     private String serverPort;      //...  } 

When starting up the application with mvn spring-boot:run, I get the following exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myController': Injection of autowired dependencies failed; nested exception is  org.springframework.beans.factory.BeanCreationException: Could not autowire field: ... String ... serverAddress; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'server.address' in string value "${server.address}" 

Both server.address and server.port cannot be autowired.

How can I find out the (local) host/address/NIC and port that a Spring Boot application is binding on?

like image 312
Abdull Avatar asked Apr 28 '15 21:04

Abdull


People also ask

How do I find my spring boot server port number?

Usually, the most straightforward way to configure the HTTP port of a Spring Boot application is by defining the port in the configuration file application. properties or application. yml. Next, let's go through the two scenarios and discuss different ways to get the port programmatically at runtime.

Where is localhost port in spring boot application?

To get the server address you can use the InetAddress class to get the local ip-address, for the port you can use ${server. port:8080} is you are using tomcat (this trick would also work for the server. address of course.

How do you get the spring boot host and port address during run time?

You can get this information via Environment for the port and the host you can obtain by using InternetAddress . Getting the port this way will only work, if a) the port is actually configured explicitly, and b) it is not set to 0 (meaning the servlet container will choose a random port on startup).


1 Answers

IP Address

You can get network interfaces with NetworkInterface.getNetworkInterfaces(), then the IP addresses off the NetworkInterface objects returned with .getInetAddresses(), then the string representation of those addresses with .getHostAddress().

Port

If you make a @Configuration class which implements ApplicationListener<EmbeddedServletContainerInitializedEvent>, you can override onApplicationEvent to get the port number once it's set.

@Override public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) {     int port = event.getEmbeddedServletContainer().getPort(); } 
like image 123
Kiwi Avatar answered Sep 20 '22 18:09

Kiwi