How to configure embedded Tomcat integrated with Spring to listen requests to IP address, besides localhost?

JavaSpringTomcatSpring MvcSpring Boot

Java Problem Overview


I am trying to run the example from the spring guide : https://spring.io/guides/gs/rest-service/"> Building a RESTful Web Service .

It works well if I open localhost:8080/greeting.

But it cannot make connection if I open either 192.168.1.111:8080/greeting, or 140.112.134.22:8080/greeting instead, despite both IPs are actually used by my computer on the internet.

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)?

Thanks! :)

Java Solutions


Solution 1 - Java

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:

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.

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.

Solution 2 - Java

Simply add in application.properties file:

server.address=0.0.0.0

Solution 3 - Java

  1. Try adding this to java parameters: -Djava.net.preferIPv4Stack=true -Djava.net.preferIPv6Addresses=false

  2. Run the query from curl: curl -vvv -X GET "http://192.168.1.111:8080/greeting"

If the 1. doesn't help, then most likely your firewall / proxy prevent the connection. Curl should give proper indication of that

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3556304View Question on Stackoverflow
Solution 1 - JavaCodeChimpView Answer on Stackoverflow
Solution 2 - JavaPaulo RobertoView Answer on Stackoverflow
Solution 3 - JavaAlexander KatzView Answer on Stackoverflow