Do I need to install Tomcat separately for spring-boot project?

Viewed 1605

I have a spring-boot project and I have the following setting:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <!--<scope>provided</scope>-->
        </dependency>

The reason I am asking this is that at one time I can run my web (war package) application in both IntelliJ IDE and at a terminal (mvn spring-boot:run) to start the application, then I can use localhost to send http request to restful service. I didn't install Tomcat separately.

After quite a while, I can still run my web application successfully in IntelliJ, but can't run it through "mvn spring-boot:run". I guess it is due to some changes in my pom file. The error message seems to be Tomcat related:

[WARNING] 
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run (AbstractRunMojo.java:506)
    at java.lang.Thread.run (Thread.java:748)
Caused by: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh (EmbeddedWebApplicationContext.java:137)
    at org.springframework.context.support.AbstractApplicationContext.refresh (AbstractApplicationContext.java:536)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh (EmbeddedWebApplicationContext.java:122)
    at org.springframework.boot.SpringApplication.refresh (SpringApplication.java:761)
    at org.springframework.boot.SpringApplication.refreshContext (SpringApplication.java:371)
    at org.springframework.boot.SpringApplication.run (SpringApplication.java:315)
    at org.springframework.boot.SpringApplication.run (SpringApplication.java:1186)
    at org.springframework.boot.SpringApplication.run (SpringApplication.java:1175)
    at com.jd.jnlu.qe.boot.JnluQEWebStart.main (JnluQEWebStart.java:22)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run (AbstractRunMojo.java:506)
    at java.lang.Thread.run (Thread.java:748)
Caused by: org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat

Will it help if I install Tomcat on my machine to be able to run it through 'mvn spring-boot:run'? Also, currently I didn't install Tomcat or Apache server, should I can the web application successfully in IntelliJ?

1 Answers

You have commented out the scope provided for tomcat. In that state, it defaults to compile scope which means it will be available on the classpath when you launch your app (no matter which method you use).

When you uncomment it, and put it to scope provided, it means it will only be available at compile time and you expect the JDK or a container to provide the dependency on the classpath. Which makes sense if you deploy it to a stand alone instance of tomcat.

Like JB Nizet already said, Spring-boot uses and starts an embedded tomcat container. But in order to do so, it needs the dependency!

As suggested by others, you should read the documentation of spring-boot to learn how it works. To get you started: here is a nice how-to that explains the basics of running a spring-boot app using maven https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started-first-application.html#getting-started-first-application-run Hopefully you are able to re-produce it from there.

Note that spring-boot-starter-web depends on spring-boot-starter-tomcat!

Related