0

I am using Swagger-UI and Codegen to generate my APIs and then I am using Spring Boot and Hibernate in my application.

When I build my application and run via Maven command mvn spring-boot:run, my application runs and Swagger UI is displayed. But when I create the WAR file and deploy it to Tomcat server, I am not able to access the application. I do not see any errors in Catalina logs. Any advice on what could be going wrong?

pom.xml:

<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 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.datadivers</groupId>
    <artifactId>swagger-spring</artifactId>
    <packaging>war</packaging>
    <name>swagger-spring</name>
    <version>1.0.0</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.5</version>
</parent>

    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <springfox-version>2.9.2</springfox-version>
    </properties>

    <dependencies>
        <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>

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

        <!--SpringFox dependencies -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${springfox-version}</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${springfox-version}</version>
        </dependency>

        <dependency>
            <groupId>com.github.joschi.jackson</groupId>
            <artifactId>jackson-datatype-threetenbp</artifactId>
            <version>2.6.4</version>
        </dependency>

        <!-- Bean Validation API support -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
        </dependency>



        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
            </dependency>
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            </dependency>

    </dependencies>

    <build>
        <sourceDirectory>src</sourceDirectory>
        <finalName>bankingapi</finalName>
        <plugins>
            <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.1.5.RELEASE</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        </plugins>
    </build>
</project>

Spring Boot application:

imports...

@SpringBootApplication
@EnableSwagger2
@EnableJpaRepositories(basePackages = "com.datadivers.repository")
@EntityScan(basePackages = "com.datadivers.model")
@ComponentScan(basePackages = { "io.swagger", "io.swagger.configuration", "com.datadivers.api", "com.datadivers.service"})
public class Swagger2SpringBoot extends SpringBootServletInitializer{
    
    
    public static void main(String[] args) {
        SpringApplication.run(Swagger2SpringBoot.class, args);
    }
    
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Swagger2SpringBoot.class);
    }
}
andrewJames
  • 19,570
  • 8
  • 19
  • 51
Vikas
  • 11
  • 1
  • 3
  • Look at the answer to the same question https://stackoverflow.com/a/47824722/5790043 – Alex Fedorov May 09 '21 at 18:29
  • Thanks @AlexFedorov. I had looked into that and when I add the context path to the link, I still do not see anything success. My URL: http://localhost:8080/bankingapi/swagger-ui.html – Vikas May 09 '21 at 19:24
  • Which version of Tomcat are you using? – Piotr P. Karwasz May 09 '21 at 20:41
  • @PiotrP.Karwasz: I am using _apache-tomcat-10.0.5_ – Vikas May 09 '21 at 20:50
  • Spring 5 requires a Java EE 8 server (it provides a [`javax.servlet.ServletContainerInitializer`](https://jakarta.ee/specifications/servlet/4.0/apidocs/javax/servlet/servletcontainerinitializer)), while Tomcat 10 is a Jakarta EE 9 server (it searches for a `jakarta.servlet.ServletContainerInitializer`): see [this question](https://stackoverflow.com/q/66806582/11748454). Downgrade to Tomcat 9.0 and it should work. – Piotr P. Karwasz May 09 '21 at 21:02
  • Thank you @PiotrP.Karwasz. I saw the link you provided and downgraded it to 9.0 and the spring application came up without any issues. However, I had a slight issue with the performance of my application. Hence I tried version 8.0 and everything went smooth. – Vikas May 10 '21 at 00:27
  • Does this answer your question? [Tomcat 10.0.4 doesn't load servlets (@WebServlet classes) with 404 error](https://stackoverflow.com/questions/66806582/tomcat-10-0-4-doesnt-load-servlets-webservlet-classes-with-404-error) – Piotr P. Karwasz May 14 '21 at 07:56

3 Answers3

0

The suggestion from @PiotrP.Karwasz worked for me. Hence submitting this as the answer to my problem. Thank you for the help.

Spring 5 requires a Java EE 8 server (it provides a javax.servlet.ServletContainerInitializer), while Tomcat 10 is a Jakarta EE 9 server (it searches for a jakarta.servlet.ServletContainerInitializer): see this question. Downgrade to Tomcat 9.0 and it should work.

Vikas
  • 11
  • 1
  • 3
0

In Intellij idea

  1. in project structure, you can create artifacts
  2. pay attention to output directory this path must be excluded in Module section
  3. now you can add war file to Deployment Section in Configuration Section

if you have security checked the access when input url swagger in to browser

Douran
  • 1
-2

Try this guideline https://www.baeldung.com/spring-boot-war-tomcat-deploy#creating-a-spring-boot-war

add a Tomcat dependency

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

and add SpringBootServletInitializer like in example

@SpringBootApplication
public class SpringBootTomcatApplication extends SpringBootServletInitializer {
}
  • Thank you @AlexFedorov for the suggestion, but I had this on my pom.xml already. After trying the suggestion from PiotrP.Karwasz, I was able to get my application to work. – Vikas May 10 '21 at 00:30