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

后端 未结 9 1231
野性不改
野性不改 2020-12-09 02:27

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 a

9条回答
  •  清歌不尽
    2020-12-09 03:12

    One solution mentioned in a reply by @M. Deinum is one that I've used in a number of Akka apps:

    object Localhost {
    
      /**
       * @return String for the local hostname
       */
      def hostname(): String = InetAddress.getLocalHost.getHostName
    
      /**
       * @return String for the host IP address
       */
      def ip(): String = InetAddress.getLocalHost.getHostAddress
    
    }
    

    I've used this method when building a callback URL for Oozie REST so that Oozie could callback to my REST service and it's worked like a charm

提交回复
热议问题