spring-boot

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?


Solution

  • 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;