首页 文章

如何在tomcat服务器上部署spring boot Web应用程序

提问于
浏览
23

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

5 回答

  • 4

    以下是有关如何将 Spring Boot App部署为 war 文件的两个好文档 .

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

    根据此文档的步骤 -

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

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

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

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

    还有一种方式 -

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

    脚步 -

    • jar 包装更改为 war .

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

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

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

    <依赖>
    <的groupId> org.springframework.boot </的groupId>
    <artifactId的> spring 引导起动的Web </ artifactId的>
    <排除>
    <排除>
    <的groupId> org.springframework.boot </的groupId>
    <artifactId的> spring 引导起动的Tomcat </ artifactId的>
    </排除>
    </排除>
    </依赖性>

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

  • 12

    Spring引导提供了将应用程序部署为支持tomcat服务器的 servlet 3.xwithout web.xml )中的传统war文件的选项 . 请参阅spring boot documentation . 我将简要介绍一下你需要做什么 .

    step 1 :修改 pom.xml 将包装更改为战争:(您已经做过)

    <packaging>war</packaging>
    

    step 2 :改变你的依赖关系

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

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

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

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

    step 4 :运行maven build来创建war: clean install 步骤5:在tomcat中部署生成的war文件 web-service.war 并在浏览器中请求url http://<tomcat ip>:<tomcat port>/web-service/hello

    你应该得到 Hello World .

    Note: 你也可以删除多余的依赖关系@Ali Dehghani说 .

  • 32

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

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

    Note1:pom.xml 中删除冗余依赖项,如:

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

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

    Note2: 让jar不战

  • 0

    将 spring 启动 jar 转换为 spring 启动战争的过程记录在:http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build-tool-plugins-maven-packaging长话短说,按照您在示例中的方式设置启动器类,然后在.pom文件中将包装从jar切换到war . 此外,您需要将spring-boot-starter-tomcat依赖项设置为已提供 . 再一次,该过程在上面的链接中以完整的形式记录 . 有关此主题的更多信息,请参见 spring io指南,"Converting a Spring Boot JAR Application to a WAR",可在https://spring.io/guides/gs/convert-jar-to-war/获取 . 如果我可以获得任何进一步的帮助,请告诉我,我会帮助您 .

  • 0

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

    • 使pom中的包装成为WAR .
    <packaging>war</packaging>
    
    • 向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>
    
    • 我用Application类来保存main() . Main有配置代码,因此可以注入 EntityManager 等 . 此 EntityManager 使用来自 ApplicationContextpersistence.xml 文件的信息来获取持久性信息 . 在SpringBoot下工作正常,但在Tomcat下没有 . 实际上在Tomcat下没有调用 Main() . Application类扩展 SpringBootServletInitializer .

    • 以下方法添加到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 工作 .

    • 所需的注释是:
    @EnableAutoConfiguration
    @ComponentScan
    //@SpringBootApplication will consist of both of these and @Configuration
    
    • 某些网址现在可能需要更改根上下文以包含
    <artifactId>contextname</artifactId>.
    

    希望有所帮助

相关问题