Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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! :)

like image 718
user3556304 Avatar asked May 30 '14 03:05

user3556304


People also ask

Can we configure embedded tomcat server in Spring boot?

We can start Spring boot applications in an embedded tomcat container that comes with some pre-configured default behavior via a properties file. In this post, we will learn to modify the default tomcat configurations via overriding respective properties in application.

Which port does Embedded Server listen on by default for HTTP request?

By default, the embedded server starts on port 8080.

Can you change the port of embedded tomcat server in Spring boot if yes how?

Another way to change the port of embedded tomcat in the Spring Boot application is by specifying the server. port property in the resource file. For example, if you want your Spring boot application to listen on port 8080, then you can specify server. port=8080 on the application.


2 Answers

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.

like image 162
CodeChimp Avatar answered Oct 03 '22 23:10

CodeChimp


Simply add in application.properties file:

server.address=0.0.0.0

like image 27
Paulo Roberto Avatar answered Oct 04 '22 01:10

Paulo Roberto