others-how to solve 'Port 8080 was already in use' problem in SpringBoot apps

Problem

When we develop 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 which consumes the webservice

image-20201120140146339

When we start both applications with gradle bootRun , we got this problem :

> Task :app:bootRun FAILED

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.2.RELEASE)

2020-11-18 18:01:35.477  INFO 5494 --- [           main] com.bswen.app1.ConsumingRestApplication  : Starting ConsumingRestApplication on MacBook-Pro-bswen.local with PID 5494 (/Users/bswen/bswen-springboot23/app1/build/classes/java/main started by bswen in /Users/bswen/bswen-springboot23/app1)
2020-11-18 18:01:35.479  INFO 5494 --- [           main] com.bswen.app1.ConsumingRestApplication  : No active profile set, falling back to default profiles: default
2020-11-18 18:01:36.088  INFO 5494 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-11-18 18:01:36.095  INFO 5494 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-11-18 18:01:36.096  INFO 5494 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-11-18 18:01:36.139  INFO 5494 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-11-18 18:01:36.139  INFO 5494 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 623 ms
2020-11-18 18:01:36.279  INFO 5494 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-11-18 18:01:36.381  WARN 5494 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'; nested exception is org.springframework.boot.web.server.PortInUseException: Port 8080 is already in use
2020-11-18 18:01:36.382  INFO 5494 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
2020-11-18 18:01:36.384  INFO 5494 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-11-18 18:01:36.393  INFO 5494 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-11-18 18:01:36.400 ERROR 5494 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Web server failed to start. Port 8080 was already in use.

Action:

Identify and stop the process that's listening on port 8080 or configure this application to listen on another port.


Execution failed for task ':app1:bootRun'.
> Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1

Environment

  • gradle 6.2.x
  • spring boot 2.3
  • IntelliJ IDEA 2019.3

Reason

Because the restful client is also a spring boot web application, it listens on port 8080 too! So the conflict occurs.

image-20201120141030468

Solution

The key point is disable the web server in the spring boot restful client app. You can do as follows:

  1. Open the src/main/resources/application.yml or application.properties

  2. If you are using application.yml,then add these lines:

    spring:
      main:
        web-application-type: NONE
    
  3. If you are using application.properties,then add these lines:

    spring.main.web-application-type=NONE
    

Restart the apps, everything works!

All the codes are shared in github.com, you can access the code here.