获取Java项目启动Ip和端口号


import org.springframework.beans.factory.annotation.Value;
import java.net.InetAddress;

@Component
public class ServerConfig {

@Value("${server.port}")
private int serverPort;

  public String getUrl() {
    InetAddress address = null;
    try {
      address = InetAddress.getLocalHost();
    } catch (UnknownHostException e) {
      e.printStackTrace();
    }
    return "http://"+address.getHostAddress() +":"+this.serverPort;
  }