How to configure embedded Tomcat integrated with Spring to listen requests to IP address, besides localhost?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When developing Spring applications, you often use an embedded Tomcat server to ease development and testing. By default, Spring Boot's embedded Tomcat will bind to localhost. However, you might need to configure it to listen to a specific IP address, especially when running on remote servers or while testing from different machines on the same network. This article explains how to configure an embedded Tomcat server integrated with a Spring application to listen to requests from an IP address other than localhost.
Configuration Overview
Spring Boot applications are typically configured using an application.properties or application.yml file located in the src/main/resources directory. To change the default behavior of the embedded Tomcat server, you'll need to modify these configurations.
Using Application Properties
For a Spring Boot application configured via application.properties, you can specify a different IP address by setting the server.address property. By default, Spring Boot listens on localhost (127.0.0.1). To configure it to listen on a specific IP address, add or modify the following entry in your application.properties:
Here:
server.address: Specifies the IP address on which the application should listen.server.port: Specifies the port on which Tomcat listens. Ensure the chosen port is not used by another application.
Using Application YAML
Alternatively, you can configure your application using a application.yml file. Here is the equivalent configuration:
Embedded Tomcat Configuration Programmatically
In some cases, you may want to configure the embedded Tomcat programmatically. Changes can be made directly in the application's main class or a configuration class, using a WebServerFactoryCustomizer bean. For example:
Networking Considerations
When you change the binding IP, make sure your network configuration allows it:
- Check that your machine's firewall settings allow incoming traffic on the specified IP and port.
- Ensure no other application is bound to the same IP-Port combination.
- If you are running the Spring application in a cloud or containerized environment, additional configurations such as security groups (in AWS) or Docker network settings may be necessary.
Testing the Configuration
After configuring the address, you'll want to test it:
- Ping the IP: Ensure that the IP address is accessible from your client machine. Use
pingor a similar tool.
- Run the Application: Ensure the application starts without errors.
- Access from Browser or API Client: Try accessing your application by entering
http://192.168.1.100:8080in your web browser or using a tool like Postman orcurl.
Troubleshooting
- Error Binding Port: If you receive an error stating the address or port is already in use, ensure no other service is occupying it.
- Connection Refused: Double-check firewall rules and make sure
server.addressis correct. - Network Visibility: If unable to access the IP externally, ensure that the network setup (subnets, routing) correctly routes traffic.
Summary
| Configuration Method | File Example | Code Example | Network Configuration |
| Properties | server.address=192.168.1.100
server.port=8080 | factory.setAddress(InetAddress.getByName("192.168.1.100")) | Ensure firewall permits incoming traffic. |
| YAML | address: 192.168.1.100
port: 8080 | N/A | Ensure no conflict with existing services. |
| Programmatic | N/A | Java code snippet provided | Verify network settings and accessibility. |
By following these steps and guidelines, you can successfully configure Spring Boot's embedded Tomcat to listen on any specific IP address, which significantly aids in both development and testing scenarios where access over the network is necessary.

