Java 除了localhost之外,如何配置与Spring集成的嵌入式Tomcat来侦听对IP地址的请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23946369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to configure embedded Tomcat integrated with Spring to listen requests to IP address, besides localhost?
提问by user3556304
I am trying to run the example from the spring guide : Building a RESTful Web Service .
我正在尝试运行 spring 指南中的示例:构建 RESTful Web 服务。
It works well if I open localhost:8080/greeting.
如果我打开localhost:8080/greeting,效果很好。
But it cannot make connection if I open either 192.168.1.111:8080/greeting, or 140.112.134.22:8080/greetinginstead, despite both IPs are actually used by my computer on the internet.
但是,如果我打开192.168.1.111:8080/greeting或140.112.134.22:8080/greeting则无法建立连接,尽管我的计算机在互联网上实际使用了这两个 IP。
Could someone suggest me how to configure the embedded Tomcat in Spring to accept HTTP request on other IP addresses, besides localhost(that is, 127.0.0.1)?
有人可以建议我如何在 Spring 中配置嵌入式 Tomcat 以接受除 localhost(即 127.0.0.1)之外的其他 IP 地址上的 HTTP 请求?
Thanks! :)
谢谢!:)
采纳答案by CodeChimp
In order to specify a which IP you want Tomcat to bind too, I believe you can simply add the following to your application.properties:
为了指定您也希望 Tomcat 绑定的 IP,我相信您可以简单地将以下内容添加到您的 application.properties 中:
server.address=<your_ip>
server.port=<your_port>
Replacing <your_ip>
with the IP address you want it to listen on. This, and other basic properties, can be found in the Spring Boot Reference Guide, Appendix A.
替换<your_ip>
为您希望它侦听的 IP 地址。这个和其他基本属性可以在Spring Boot 参考指南,附录 A 中找到。
The other way to configure the embedded Tomcat is to create a custom configurer in code by implementing the EmbeddedServletContainerCustomizer
interface. You can read more about this in the Spring Boot Reference Guide, Section 55.5-55.8.
另一种配置嵌入式Tomcat的方法是通过实现EmbeddedServletContainerCustomizer
接口在代码中创建自定义配置器。您可以在Spring Boot 参考指南的第 55.5-55.8 节中阅读有关此内容的更多信息。
回答by Rene Enriquez
I solved this setting the IP address in the /etc/hosts file in this way:
我通过这种方式解决了在 /etc/hosts 文件中设置 IP 地址的问题:
IP hostmane
Like:
喜欢:
172.x.x.x my-hostname
回答by Paulo Roberto
Simply add in application.properties file:
只需在 application.properties 文件中添加:
server.address=0.0.0.0
server.address=0.0.0.0