我想知道tomcat用于我的Spring应用程序的端口。
获取我使用的地址:
InetAddress inetAddress = InetAddress.getLocalHost();
String host = inetAddress.getHostAddress()
但内部任何地方都没有端口信息。我内部未指定application.properties
或server.host
server.port
,因此environment.getProperty("server.port")
会返回null
。
我也试过this解决方案:
@Inject
private ServerProperties serverProperties;
...
serverProperties.getPort()
但它也会返回null
我读here我应该使用Socket#getLocalPort()
,但我不知道如何获取Socket
并且新实例返回默认值:-1
在捕获请求时还有一种方法可以做到:
ServletRequest.getLocalAddr();
ServletRequest.getLocalPort();
但在我的情况下它没用。