Home > OS >  Question on spring-web application running in embedded tomcat within IDE
Question on spring-web application running in embedded tomcat within IDE

Time:08-16

  1. For running the Spring web-application as Maven project, I need to add spring-boot-starter-tomcat as dependency in pom.xml. But for gradle project it is not required in build.gradle. Why is that so? I am running both in Intellij.

  2. Running the Spring web-application as Maven project, if scope is specified as 'provided' in spring-boot-starter-tomcat dependency, the application starts without error and just ends. I need to comment scope 'provided' to run continuously as server. This is the console output on scope 'provided':

Starting CourseidServiceApplication using Java 17.0.4 on ES-LAPTOP-876 with PID 21352 (C:\ProjWS\courseid-service\target\classes started by kakoli.sen in C:\ProjWS\courseid-service)
2022-08-14 10:28:06.919  INFO 21352 --- [           main] c.p.l.s.s.c.CourseidServiceApplication   : No active profile set, falling back to 1 default profile: "default"
2022-08-14 10:28:07.574  INFO 21352 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-08-14 10:28:07.641  INFO 21352 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 56 ms. Found 1 JPA repository interfaces.
2022-08-14 10:28:08.300  INFO 21352 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2022-08-14 10:28:08.354  INFO 21352 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.6.10.Final
2022-08-14 10:28:08.520  INFO 21352 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2022-08-14 10:28:08.686  INFO 21352 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2022-08-14 10:28:09.050  INFO 21352 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2022-08-14 10:28:09.066  INFO 21352 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
2022-08-14 10:28:09.787  INFO 21352 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2022-08-14 10:28:09.795  INFO 21352 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2022-08-14 10:28:10.309  INFO 21352 --- [           main] c.p.l.s.s.c.CourseidServiceApplication   : Started CourseidServiceApplication in 3.86 seconds (JVM running for 4.392)
After
2022-08-14 10:28:10.317  INFO 21352 --- [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2022-08-14 10:28:10.320  INFO 21352 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2022-08-14 10:28:10.333  INFO 21352 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

========
pom.xml
========
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.pearson.ltg.sms.services.courseid</groupId>
    <artifactId>courseid-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>courseid-service</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

CodePudding user response:

I don't think you need a tomcat starter here:

You already have

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
</dependency>

By default it plugs the embedded tomcat and spring MVC so no need to other dependencies

Open Spring Initializr project and create a sample project, specify the webmvc and see what does it produce...

  • Related