spring - 如何部署 swagger 生成的 spring 代码(Tomcat)

标签 spring tomcat swagger

我使用SwaggerCodeGen给出的例子通过SpringBoot创建了一个Server。 我用 maven 构建项目并在本地运行它。一切正常。

现在我想在 tomcat(版本 7)上部署这个项目。 所以我把包装从jar换成了war

<packaging>war</packaging>

并将 *.war 文件移动到 tomcat\webapps 文件夹

我试着跑

localhost:8080/app

返回 404

同理

localhost:8080/app/swagger-ui.html
localhost:8080/v1/app/
localhost:8080/v1/app/api-docs

不幸的是,我没有使用 tomcat 的经验。
该项目不包含 web.xml。有必要吗? 我需要创建一个 Servlet 吗?

如有任何帮助,我们将不胜感激。

最佳答案

在您的 POM 中,您需要:

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

<build>
    <plugins>

        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>1.5.9</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
    </plugins>

</build>

您的 SpringBoot 应用程序也应该如下所示:

public class SpringBootServiceApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootServiceApplication .class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootServiceApplication .class);
    }

}

关于spring - 如何部署 swagger 生成的 spring 代码(Tomcat),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47866989/

相关文章:

java - 为什么 AbstractApplicationContext 会为 LogFactory 抛出 NoClassDefFoundError?

Spring 启动服务器错误

java - 无法创建池 Async Jersey+Spring+Tomcat 的初始连接

amazon-web-services - 使用命令行工具将 Swagger API 导入 AWS API Gateway

java - 需要以编程方式配置 Hibernate sessionFactory 结果为 'sessionFactory' 或 'hibernateTemplate'

java - HTTP 状态 404 – 未找到 -hibernate-spring mvc--

sql - 如何在 Microsoft Visual FoxPro 9 中执行自定义函数?

java - Swagger2 更改 Swagger Ui 的基本路径

java - 无法使用 tomcat : org. opengis.referencing.NoSuchAuthorityCodeException 获取 CRS

tomcat - 如何根据tomcat日志计算PV,ip,PU