When developing Spring Boot RESTful service and client (consumer) applications, the architecture is as follows:
Spring Boot RESTful service: A web service that listens on port 8080.
Spring Boot RESTful client (consumer): A RESTful client that consumes the web service.
When starting both applications with gradle bootRun, the following problem occurs:
Environment
Gradle: 6.2.x
Spring Boot: 2.3
IntelliJ IDEA: 2019.3
Reason
The RESTful client is also a Spring Boot web application, which listens on port 8080 by default. This causes a port conflict.
Solution
The key point is to disable the web server in the Spring Boot RESTful client application. You can do this as follows:
Open the src/main/resources/application.yml or application.properties file.
If you are using application.yml, add these lines:
If you are using application.properties, add these lines:
Restart the applications, and everything should work correctly.
All the code is shared on GitHub. You can access it here.
Summary
In this post, we explored how to resolve the “Port 8080 was already in use” error in Spring Boot applications. The issue arises when both the RESTful service and client applications attempt to use the same port. By disabling the web server in the client application, you can avoid port conflicts and ensure both applications run smoothly. This solution is particularly useful when developing microservices or client-server architectures in Spring Boot.
Final Words + More Resources
My intention with this article was to help others who might be considering solving such a problem.
So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by
email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope: