我使用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/