如何在Tomcat服务器上部署Spring Boot Web应用程序

28

我创建了一个Spring Boot Web应用程序,但是我无法将Spring Boot Web应用程序WAR文件部署到Tomcat上,而且我可以将其作为Java应用程序运行。如何将Spring Boot应用程序作为Web服务在Tomcat上运行。我正在使用以下代码。如果可以在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);
    }

}

以下是rest控制器的代码

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

如果我将其导出为jar文件,则无法在远程Tomcat上部署,并且无法从浏览器中调用此应用程序。 - satish
为什么需要一个单独的Tomcat来从浏览器访问应用程序?Spring Boot本身包含嵌入式服务器来实现该功能。 - Tom Sebastian
如果您仍需要它作为一场战争,请查看我的答案。 - Tom Sebastian
6个回答

34

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

你可以按照这个 Spring Boot howto-traditional-deployment 文档进行操作 -

按照该文档的步骤 -

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

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

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

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

还有一种方法 -

请查看此Spring IO文档,其中详细说明了如何将Spring Boot应用程序部署到应用程序服务器。

步骤 -

  1. jar打包方式更改为war

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

  3. 通过扩展SpringBootServletInitializer并覆盖configure方法,向应用程序添加Web入口点。

  4. 删除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-beansspring-webmvc依赖项。 spring-boot-starter-web 依赖项将包括这些依赖项。

我已经按照你说的进行了更改,但它抛出了“选择不能在服务器上运行”的错误,这意味着它无法识别war文件。 - satish
好的。这是另一篇Spring Boot传统部署文档 - https://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html 。你可以尝试这种方法! - Omkar Puttagunta
有没有办法使 POM 的某些部分与生命周期有条件地关联起来? - roberto tomás
为什么要使用war而不是jar?请查看以下问题:http://stackoverflow.com/questions/43303849/spring-boot-build-project-for-run-on-another-system-not-local-system - Mahdi

12
Spring Boot提供了将应用程序部署为传统的WAR文件的选项,支持Tomcat服务器的Servlet 3.x(无需web.xml)。请参见Spring Boot文档。我会简要介绍你需要在这里做什么。
步骤1:修改pom.xml文件,将打包方式改为war(您已经完成此步骤)。
<packaging>war</packaging>

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

步骤三:在<build>标签下的pom.xml文件中修改您的war名称(如果需要避免版本细节附加到war名称中)。
<build>
    <finalName>web-service</finalName>
.....

步骤 4: 运行 Maven 构建以创建 war 文件: clean install 步骤 5: 在 Tomcat 中部署生成的 war 文件web-service.war, 并在浏览器中请求 URLhttp://<tomcat ip>:<tomcat port>/web-service/hello 你应该会看到Hello World 注意: 你也可以像 @Ali Dehghani 提出的那样删除冗余的依赖。

我得到了“无法在任何服务器上运行选择”这个错误。 - satish
你是在使用 Eclipse 运行吗? - Tom Sebastian
是的,我正在使用Eclipse运行。 - satish
你能发一下你的电话号码给我吗? - satish

4

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

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

注意1:从您的pom.xml中删除冗余依赖项,例如:

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

它们是 Spring Boot Starter 包 的一部分。

注2: 制作 jar 而不是 war。


1
如果我打成 jar 包,就无法部署到远程的 Tomcat 服务器上,这样就不可能从 web 浏览器中调用。 - satish
你可以在Linux或者Windows和Mac上将Spring Boot应用程序安装为服务。我想说的是,下一次在部署Java应用程序(或任何应用程序)时,请多考虑一下。这种端口绑定式的应用程序部署方式具有许多优点,并且更符合当今的最佳实践。 - Ali Dehghani
1
我知道有时候我们不得不这样做是因为某些疯狂的企业文化。 - Ali Dehghani
1
如何将应用程序作为服务在远程Tomcat服务器上使用,只需选择WAR文件选项即可。 - satish

0

在本地区域网络上部署Spring Boot Web应用程序到Tomcat服务器的步骤如下:

  1. 在项目的application.properties文件中添加server.port = 8080和server.address =(您的PC IP地址)。请注意:不要将IP地址放在此处使用的括号内,只是为了在括号内显示IP地址。

  2. 然后制作Spring Boot应用程序的war文件,您可以在EclipseVS Code中制作它,无论您使用哪个IDE。

如何在VS Code中生成war文件:

在maven文件夹中打开maven文件夹,您会找到您的项目并单击该文件夹,然后有一个名为“clean”的选项,请单击“clean”它将清理名为target的项目文件夹,之后再次单击同一项目,在“clean”下方您会发现一个名为“install”的选项,请单击它,它将在您的项目中src文件夹下新创建一个名为target的文件夹,并打开此目标文件夹,您会发现两个war文件,一个是简单的,另一个是扩展的,具有原始文件,从这些文件中,您必须上传除原始文件以外的war文件。
  1. 通过在浏览器中运行tomcat并转到tomcat中的管理器应用程序,在tomcat服务器webapp文件夹上上传war文件。
  2. 之后,请从任务栏中的防火墙和保护中关闭您系统中的公共防火墙网络。
  3. 转到同一局域网上的其他计算机,并通过http://IP_address:8080/yourwebsite_url访问应用程序URL。
    您的网站URL将与您在tomcat服务器上上传的war文件相同。

0
将Spring Boot jar转换为Spring Boot war的过程在以下文档中有详细说明:http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build-tool-plugins-maven-packaging 简而言之,按照您的示例设置启动类,然后在.pom文件中将打包方式从jar切换到war。此外,您需要将spring-boot-starter-tomcat依赖项设置为provided。再次提醒,完整的过程在上面的链接中有详细说明。 关于这个主题的更多信息可以在Spring IO指南“将Spring Boot JAR应用程序转换为WAR”中找到,该指南可在https://spring.io/guides/gs/convert-jar-to-war/上获得。 如果您需要进一步的帮助,请告诉我,我会尽力协助您。

0

我曾面临过这个问题。上述大部分都是很好的建议。我的问题是最初在Pivotal TC服务器上部署。

  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 使用 ApplicationContext 和 persistence.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);
    }
    

只有最后一行是必需的 - 其他代码之前都在main()中,现在移动到这里以使EntityManager注入正常工作。

  1. 需要的注释是:

    @EnableAutoConfiguration
    @ComponentScan
    // @SpringBootApplication 将包含这两个和 @Configuration
    
  2. 现在可能需要将一些 URL 的根上下文更改为包含

    <artifactId>contextname</artifactId>。
    
希望这有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接