无法使用swagger、spring和hibernate访问tomcat上的应用程序

wyyhbhjk  于 2021-10-10  发布在  Java
关注(0)|答案(2)|浏览(437)

我使用swagger ui和codegen生成API,然后在应用程序中使用spring boot和hibernate。
当我构建应用程序并通过maven命令运行时 mvn spring-boot:run ,我的应用程序运行并显示Swigger ui。但是,当我创建war文件并将其部署到tomcat服务器时,我无法访问该应用程序。我在catalina日志中没有看到任何错误。对可能出现的问题有什么建议吗?
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启动应用程序:

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);
    }
}
jv4diomz

jv4diomz1#

@piotrp.karwasz的建议对我很有用。因此,将此作为我问题的答案提交。谢谢你的帮助。
Spring5需要一个JavaEE8服务器(它提供一个javax.servlet.servletcontainerinitializer),而Tomcat10是一个JakartaEE9服务器(它搜索一个jakarta.servlet.servletcontainerinitializer):请看这个问题。降级到tomcat 9.0,它应该可以工作。

nfzehxib

nfzehxib2#

试试这个指南https://www.baeldung.com/spring-boot-war-tomcat-deploy#creating- Spring 战靴
添加tomcat依赖项

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

SpringBootServletInitializer 比如说

@SpringBootApplication
public class SpringBootTomcatApplication extends SpringBootServletInitializer {
}

相关问题