john
john

Reputation: 707

How to get server ip address in Spring Boot Environment?

I'm using Spring Boot Environment to get the server port as follows

@Autowired
Environment environment; 

environment.getProperty("local.server.port") 

It works

However, I can't figure out how to get the ip

I tried these

environment.getProperty("local.server.address") 
environment.getProperty("local.server.ip") 
environment.getProperty("local.server.host")  
// and many other combinations but can't make it to work 

What is the property name for the ip?

Upvotes: 3

Views: 11282

Answers (1)

josephtaylor.art
josephtaylor.art

Reputation: 132

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;

Upvotes: 4

Related Questions