76

I am trying to run the example from the spring guide : 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! :)

Oliver Drotbohm
  • 80,157
  • 18
  • 225
  • 211
user3556304
  • 1,027
  • 1
  • 10
  • 14
  • If you just following the Tutorial link you have posted on Building Web Services, then you Web Service is not running in an Embedded Tomcat or any web application server for that matter. It is running on a Standalone java application which a main method. You need to make a WAR file and deploy in a tomcat server. – shazin May 30 '14 at 03:11
  • Does this help? http://stackoverflow.com/questions/18617/how-do-you-configure-tomcat-to-bind-to-a-single-ip-address-localhost-instead-o – jamesmortensen May 30 '14 at 03:16
  • shazin has likely hit the nail on the head, but there are good diagnostics that will solve this sort of problem.Why not use the netstat utility to see if you are publishing a service on port 8080? If you are, try telnet to the IP and port. These are basic tools for a web developer - you need to become familiar. They are available on Window and Linux. – kiwiron May 30 '14 at 06:36
  • 6
    @user3467488, You are both incorrect. The tutorial he linked is using Spring Boot, which allows you to create a executable JAR with an embedded app server, among other things. The default is Tomcat, but you can swap it out for Jetty by simply including the Jetty JARs instead of the Tomcat JARs. It's a very interesting platform, and provides a quick way to get a running Spring application together. You can optionally turn your Spring Boot app into a WAR, but the tutorial he is linking to does not go into that at all. – CodeChimp May 30 '14 at 11:11

3 Answers3

112

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.

Tiina
  • 4,285
  • 7
  • 44
  • 73
CodeChimp
  • 8,016
  • 5
  • 41
  • 79
  • 9
    By default tomcat will bind to 0.0.0.0 and listen on all available addresses. There must be an environment restriction on the other addresses or ports if they are not working (like a firewall). – Dave Syer Jun 02 '14 at 06:19
  • @CodeChimp Thanks for the suggestion! For this [Spring guide](https://spring.io/guides/gs/rest-service/), where is the file of "application.properties" for us to modify? – user3556304 Jun 12 '14 at 18:02
  • I think in Spring Boot it will attempt to look first in the JAR, then in the classpath, then in the local directory from which you ran the application. The documentation/reference guide goes into that. – CodeChimp Jun 12 '14 at 20:04
  • this link explains how the spring boot properties are loaded. there are several places that you can pass a configuration from command line arguments to external yml or properties files. https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html – burcakulug Feb 15 '16 at 14:29
  • 4
    would have been great if location of this application.properties is specified. I had read in Spring docs a lot about application.properties that what it can do. But they don't mention in simple text- put this application.properties at your application root or something – Rockoder Mar 18 '16 at 21:00
  • saw it somewhere that it can put in src/main/resources in your application. i changed the port in there. after i restart boot cli, it works for port. will have to try for ip address. – Rockoder Mar 20 '16 at 04:40
  • You can also specify it in your application.yml as server: and then as address: under that. I put it in the environments: / development: section. – Guerry Jun 30 '17 at 17:54
  • @DaveSyer I don't think it is true any more in spring boot 2 – JJ Roman Oct 15 '20 at 21:18
  • I'm pretty sure that property is still there and still does the same thing. – Dave Syer Feb 18 '21 at 09:45
24

Simply add in application.properties file:

server.address=0.0.0.0

Paulo Roberto
  • 261
  • 2
  • 7
1
  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

Alexander Katz
  • 119
  • 1
  • 4