【问题标题】:Setup spring-boot app on tomcat在 tomcat 上设置 spring-boot 应用程序
【发布时间】:2016-08-10 20:12:39
【问题描述】:

我遵循了一个简单的指南,并使用嵌入式 tomcat 在我的本地创建了一个基于 spring-boot 的 Web 应用程序,它运行良好。下一步我试图将它部署到远程 linux tomcat 服务器上。我首先按照以下步骤操作:

(1) 更新应用程序的主类以扩展SpringBootServletInitializerSpringBootApplication是我之前的主班

public class WebInitializer extends SpringBootServletInitializer {   
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(SpringBootWebApplication.class);
}    
}

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

@Controller
public class IndexController {
    @RequestMapping("/")
    String index(){
        return "index";
    }
}

(2) 更新构建配置,使项目生成war 文件而不是jar 文件。

<packaging>war</packaging>

(3) 将嵌入式 servlet 容器依赖项标记为已提供。在我刚刚添加了一个spring-boot-starter-web之前我没有这个依赖

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

(4) 我还在application.properties中添加了应用路径

server.context-path = /springbootdemo

在运行 maven-clean 和 maven-install 之后,我得到了我的 spring-boot-web-0.0.1-SNAPSHOT.war,然后将其放入远程服务器中的 tomcat webapp 中,但我得到了 404:

http://host:port/springbootdemo
http://host:port/spring-boot-web
http://host:port/spring-boot-web/springbootdemo

我的应用程序的正确路径应该是什么?在本地运行时,它在 http://localhost:8080/ 上运行,没有应用程序名称。当我看到服务器 catalina.out 日志时,没有异常并且服务器启动。

【问题讨论】:

    标签: tomcat spring-boot


    【解决方案1】:

    server.context-path 在将war 文件部署到外部容器时无效。

    Tomcat 使用整个战争名称,减去.war 后缀,作为应用程序的上下文路径。假设您将spring-boot-web-0.0.1-SNAPSHOT.war 复制到Tomcat 的webapps 目录,您的应用程序将在http://localhost:8080/spring-boot-web-0.0.1-SNAPSHOT 可用。

    【讨论】:

    • 谢谢!这是我一直有的一个很大的疑问:) 现在我明白了。
    • 是否有机会改变tomcat配置来改变应用服务器端点?
    【解决方案2】:

    【讨论】:

      【解决方案3】:

      你可以看到我的回答here

      答案还包含一个演示项目的链接,您可以构建该项目并将其部署到 Tomcat。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-08
        • 2015-11-21
        • 1970-01-01
        • 2018-05-13
        • 2017-07-17
        • 2018-07-06
        • 1970-01-01
        • 2018-09-04
        相关资源
        最近更新 更多