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

问题描述

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

@SpringBootApplication公共类应用程序扩展 SpringBootServletInitializer {受保护的 SpringApplicationBuilder 配置(SpringApplicationBuilder 应用程序){返回 application.sources(Application.class);}public static void main(String[] args) 抛出异常 {SpringApplication.run(Application.class, args);}}

rest 控制器的以下代码

@RestController公共类HelloWorld{@RequestMapping(value = "/hello", method = RequestMethod.GET)公共响应实体得到() {return new ResponseEntity("Hello World", HttpStatus.OK);}}

在我使用的 Pom.xml 之后

<artifactId>web-service</artifactId><version>0.1.0</version><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.3.0.RELEASE</version></父母><依赖项><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></依赖><依赖><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId></依赖><依赖><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId></依赖><依赖><groupId>com.googlecode.json-simple</groupId><artifactId>json-simple</artifactId></依赖></依赖项><属性><java.version>1.6</java.version></属性><构建><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></插件></插件></build><存储库><存储库><id>spring-releases</id><url>https://repo.spring.io/libs-release</url></repository></repositories><pluginRepositories><插件库><id>spring-releases</id><url>https://repo.spring.io/libs-release</url></pluginRepository></pluginRepositories><包装>战争</包装>
解决方案

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

你可以关注这个spring boot howto-traditional-deployment 文档 -

根据本文档的步骤 -

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

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

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

    <groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><范围>提供</范围></依赖>

还有一种方式 -

看到这个 spring io 文档,其中概述了如何将 Spring Boot 应用程序部署到应用程序服务器.

步骤 -

  1. jar包装改为war.

  2. 注释掉pom.xml

  3. spring-boot-maven-plugin插件的声明
  4. 通过扩展 SpringBootServletInitializer 并覆盖 configure 方法,将 Web 入口点添加到您的应用程序中

  5. 移除spring-boot-starter-tomcat依赖并将你的spring-boot-starter-web依赖修改为

<groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><排除事项><排除><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></排除></排除项></依赖>

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

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

}

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

<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>
解决方案

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

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

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>
    

and one more way -

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

Steps -

  1. Change jar packaging to war.

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

  3. Add a web entry point into your application by extending SpringBootServletInitializer and override the configure method

  4. Remove the spring-boot-starter-tomcat dependency and modfiy your spring-boot-starter-web dependency to

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应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 05:05