如何在tomcat服务器上部署spring boot web应用程序 [英] How to deploy spring boot web application on tomcat server

查看:101
本文介绍了如何在tomcat服务器上部署spring boot web应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created spring boot web application, but I am unable to deploy spring boot web application WAR file on tomcat and I am able to run it as java application. How to run spring boot application as web service on tomcat. I am using following code. If it is possible to run on tomcat plz help me using annotations without using web.xml and with using 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);
    }

}

以下代码为rest controller

Following code for rest controller

@RestController
public class HelloWorld{

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

关注Pom.xml我正在使用

Following Pom.xml I am using

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

推荐答案

以下是关于如何将 Spring Boot 应用程序部署为 war 文件的两个好文档。

Here are two good documentations on how to deploy the Spring Boot App as a war file.

你可以按照这个春季启动 howto-traditional-deployment 文档 -

You can follow this spring boot howto-traditional-deployment documentation -

根据此文档执行的步骤 -

Steps according to this documentation -


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

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

The next step is to update your build configuration so that your project produces a war file rather than a jar file. <packaging>war</packaging>

标记提供的嵌入式servlet容器依赖项。

Mark the embedded servlet container dependency as provided.

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


还有一种方式 -

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

See this spring io documentation which outlines how to deploy the spring boot app to an application server.

步骤 -


  1. jar 打包改为 war

注释掉 spring-boot-maven-plugin pom.xml中的插件

通过扩展<$将Web入口点添加到您的应用程序中c $ c> SpringBootServletInitializer 并覆盖 configure 方法

删除 spring-boot-starter-tomcat依赖并修改你的 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-beans spring-webmvc 依赖项。 spring-boot-starter-web 依赖项将包括这些依赖项。

In your pom.xml, remove spring-beans and spring-webmvc dependencies. The spring-boot-starter-web dependency will include those dependecies.

这篇关于如何在tomcat服务器上部署spring boot web应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆