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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34506712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:43:38  来源:igfitidea点击:

How to deploy spring boot web application on tomcat server

javaspringmavenspring-mvctomcat

提问by satish

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.

我已经创建了 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);
    }

}

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);
   }
}

Following Pom.xml I am using

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

回答by Wheelchair Geek

The process of converting a spring boot jar to a spring boot war is documented at: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build-tool-plugins-maven-packagingLong story short, set your starter class the way you did in your example and then switch the packaging from jar to war in the .pom file. Furthermore, you need to set the spring-boot-starter-tomcat dependency to provided. Once again, the process is documented in it's complete form at the link above. Further information about this subject is available in the spring io guide, "Converting a Spring Boot JAR Application to a WAR" which is available at https://spring.io/guides/gs/convert-jar-to-war/If i can be of any further assistance, let me know and i will help you.

将 spring boot jar 转换为 spring boot war 的过程记录在:http: //docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build-tool-plugins-maven-packagingLong简而言之,按照您在示例中的方式设置入门类,然后在 .pom 文件中将包装从 jar 切换到 war。此外,您需要将 spring-boot-starter-tomcat 依赖项设置为提供。再一次,该过程以完整的形式记录在上面的链接中。有关此主题的更多信息,请参见 spring io 指南“将 Spring Boot JAR 应用程序转换为 WAR”,该指南可在https://spring.io/guides/gs/convert-jar-to-war/如果我可以提供任何进一步的帮助,让我知道,我会帮助你。

回答by Ali Dehghani

Mark the spring-boot-starter-tomcatdependency as provided, like:

spring-boot-starter-tomcat依赖项标记为provided,例如:

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

Note1:Remove redundant dependencies from your pom.xmllike:

注意1:从你pom.xml喜欢的东西中删除多余的依赖:

<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
</dependency>
<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
</dependency>

They are part of spring boot starter packages

它们是spring boot 启动包的一部分

Note2:Make jar not war

注2:使罐子不是战争

回答by Omkar

Here are two good documentations on how to deploy the Spring BootApp as a warfile.

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

You can follow this spring boot howto-traditional-deploymentdocumentation -

你可以按照这个 spring boot howto-traditional-deployment文档 -

Steps according to this documentation -

根据本文档的步骤 -

  1. You update your application's main class to extend SpringBootServletInitializer.

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

  3. Mark the embedded servlet container dependency as provided.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    
  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>
    

and one more way -

还有一种方式——

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

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

Steps -

脚步 -

  1. Change jarpackaging to war.

  2. Comment out the declaration of the spring-boot-maven-pluginplugin in your pom.xml

  3. Add a web entry point into your application by extending SpringBootServletInitializerand override the configuremethod

  4. Remove the spring-boot-starter-tomcat dependencyand modfiy your spring-boot-starter-webdependency to

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

In your pom.xml, remove spring-beansand spring-webmvcdependencies. The spring-boot-starter-webdependency will include those dependecies.

在您的pom.xml, 删除spring-beansspring-webmvc依赖项中。该spring-boot-starter-web依存度将包括那些依赖条件。

回答by Tom Sebastian

Spring boot provides option to deploy the application as a traditional war file in servlet 3.x(without web.xml)supporting tomcat server.Please see spring boot documentationfor this. I will brief what you need to do here.

Spring boot 提供了将应用程序部署为支持 tomcat 服务器的servlet 3.x不带 web.xml)的传统 war 文件的选项。请参阅spring boot 文档了解这一点。我将在这里简要介绍您需要做什么。

step 1: modify pom.xmlto change the packaging to war:(that you already did)

第 1 步:修改pom.xml以将包装更改为战争:(您已经这样做了)

<packaging>war</packaging>

step 2: change your dependency

第 2 步:更改您的依赖项

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

to

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

step 3:modify your war name (if you need to avoid the version details appended with the war name) in pom.xmlunder <build>tag.

第 3 步pom.xml<build>标签下修改您的战争名称(如果您需要避免附加战争名称的版本详细信息)。

<build>
    <finalName>web-service</finalName>
.....

step 4: run maven build to create war : clean installstep 5 : deploy the generated war file web-service.warin tomcat and request url in browser http://<tomcat ip>:<tomcat port>/web-service/hello

第 4 步:运行 maven build 以创建 war:clean install第 5 步:将生成的 war 文件部署到web-service.wartomcat 并在浏览器中请求 urlhttp://<tomcat ip>:<tomcat port>/web-service/hello

You should get Hello World.

你应该得到Hello World.

Note:Also you can remove redundant dependencies as @Ali Dehghani said.

注意:您也可以像@Ali Dehghani 所说的那样删除多余的依赖项。

回答by Ged

I was faced with this problem. Much of the above is good advice. My problem was to deploy on the Pivotal TC server initially.

我遇到了这个问题。以上大部分都是很好的建议。我的问题是最初部署在 Pivotal TC 服务器上。

  1. Make the packaging in the pom a WAR.

    <packaging>war</packaging>
    
  2. Add dependencies to the pom

    <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>
        <scope>provided</scope>
    </dependency>
    
  3. I used an Application class to hold the main(). Main had configuration code so that EntityManageretc could be injected. This EntityManagerused information from the ApplicationContextand persistence.xmlfiles for persistence information. Worked fine under SpringBoot but not under Tomcat. In fact under Tomcat the Main()is not called. The Application class extends SpringBootServletInitializer.

  4. The following method is added to the Application class:

    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        Application obj = new Application();
        @SuppressWarnings("resource")
        ConfigurableApplicationContext applicationContext = 
             new ClassPathXmlApplicationContext("META-INF/spring/applicationContext.xml");
         applicationContext.registerShutdownHook();
         applicationContext.getBeanFactory().autowireBeanProperties(
             obj, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
        return application.sources(Application.class);
    }
    
  1. 使 pom 中的包装成为 WAR。

    <packaging>war</packaging>
    
  2. 给pom添加依赖

    <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>
        <scope>provided</scope>
    </dependency>
    
  3. 我使用了一个 Application 类来保存 main()。Main 有配置代码,以便EntityManager可以注入等。这EntityManager使用来自ApplicationContextpersistence.xml文件的信息作为持久性信息。在 SpringBoot 下工作正常,但在 Tomcat 下不工作。实际上在Tomcat下Main()是没有被调用的。Application 类扩展了SpringBootServletInitializer.

  4. 在 Application 类中添加了以下方法:

    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        Application obj = new Application();
        @SuppressWarnings("resource")
        ConfigurableApplicationContext applicationContext = 
             new ClassPathXmlApplicationContext("META-INF/spring/applicationContext.xml");
         applicationContext.registerShutdownHook();
         applicationContext.getBeanFactory().autowireBeanProperties(
             obj, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
        return application.sources(Application.class);
    }
    

Only the last line is required - the other code was held in main()before and was moved here to get injection of the EntityManagerworking.

只需要最后一行 - 其他代码main()之前保留并移至此处以注入EntityManager工作。

  1. The annotations needed are:

    @EnableAutoConfiguration
    @ComponentScan
    //@SpringBootApplication will consist of both of these and @Configuration
    
  2. Some urls may now need the root context changing to include

    <artifactId>contextname</artifactId>.
    
  1. 需要的注释是:

    @EnableAutoConfiguration
    @ComponentScan
    //@SpringBootApplication will consist of both of these and @Configuration
    
  2. 某些 url 现在可能需要更改根上下文以包含

    <artifactId>contextname</artifactId>.
    

Hope that helps

希望有帮助