如何在tomcat服务器上部署spring boot web应用

新手上路,请多包涵

我已经创建了 spring boot web 应用程序,但我无法在 tomcat 上部署 spring boot web 应用程序 WAR 文件,我能够将它作为 java 应用程序运行。如何在 tomcat 上将 spring boot 应用程序作为 web 服务运行。我正在使用以下代码。如果可以在 tomcat 上运行,请帮助我在不使用 web.xml 和使用 web.xml 的情况下使用注释。

 @SpringBootApplication
public class Application extends SpringBootServletInitializer {

    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
       return application.sources(Application.class);
    }

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

}

以下代码用于休息控制器

@RestController
public class HelloWorld{

   @RequestMapping(value = "/hello", method = RequestMethod.GET)
   public ResponseEntity<String> get() {
       return new ResponseEntity<String>("Hello World", HttpStatus.OK);
   }
}

在我使用的 Pom.xml 之后

<groupId>org.springframework</groupId>
<artifactId>web-service</artifactId>
<version>0.1.0</version>

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

<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>
        </dependency>

        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
    </dependency>
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
    </dependency>

</dependencies>

<properties>
    <java.version>1.6</java.version>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
</pluginRepositories>
<packaging>war</packaging>

原文由 satish 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 461
1 个回答

这里有两个很好的文档,介绍如何将 Spring Boot 应用程序部署为 war 文件。

您可以遵循此 spring boot howto-traditional-deployment 文档 -

根据本文档的步骤 -

  1. 您更新应用程序的主类以扩展 SpringBootServletInitializer

  2. 下一步是更新您的构建配置,以便您的项目生成 war 文件而不是 jar 文件。 <packaging>war</packaging>

  3. 将嵌入式 servlet 容器依赖项标记为已提供。

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

还有一种方法-

请参阅此 spring io 文档,其中概述了如何将 spring boot 应用程序部署到应用程序服务器。

脚步 -

  1. jar 包装更改为 war

  2. 在你的—中注释掉 spring-boot-maven-plugin 插件的声明 pom.xml

  3. 通过扩展 SpringBootServletInitializer 并覆盖 configure 方法,将 Web 入口点添加到您的应用程序中

  4. 删除 spring-boot-starter-tomcat dependency 并将你的 spring-boot-starter-web 依赖修改为

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

在您的 pom.xml 中,删除 spring-beansspring-webmvc 依赖项。 spring-boot-starter-web 依赖项将包括这些依赖项。

原文由 Omkar Puttagunta 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题