在专用的tomcat上将spring-boot应用程序作为war文件部署不起作用 [英] Deploying a spring-boot application as a war file on a dedicated tomcat is not working

查看:80
本文介绍了在专用的tomcat上将spring-boot应用程序作为war文件部署不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个spring-boot应用程序,现在我想在专用的tomcat服务器上部署该应用程序,而不要使用嵌入式tomcat.两种部署方式都没有任何错误.

我已为 spring-boot-starter-tomcat 依赖项提供了 scope = provided .

当我运行带有嵌入式tomcat的应用程序(进行了适当的更改)时,它在点击链接http://localhost:8080/testGET时运行良好. 但是,当我在 scope = provided 上运行专用的tomcat时,点击链接http://localhost:8080/test-results-upload-1.0/testGEThttp://localhost:8080/testGET时,我得到的响应已映射到/error ./p>

请帮助我.无法理解我正在做的错误.

谢谢.

 @EnableAutoConfiguration
 @Configuration
 @EnableWebMvc
 @ComponentScan("com............controller")
 @Import(SpringMongoConfig.class)
 public class BootStrap extends SpringBootServletInitializer {

    public static void main(String[] args) {
       SpringApplication.run(BootStrap.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(
           SpringApplicationBuilder application) {
       return application.sources(BootStrap.class);
    }
}

.

  @Controller
  public class Controller {

     @Autowired(required = true)
     private IRepository config;

     @RequestMapping(value = "/testGET", method = RequestMethod.GET)
     public String testGet(HttpServletResponse response) {
        try {
           response.sendError(HttpStatus.OK.value());
           return "Application working perfectly !";
        } catch (IOException e) {
           e.printStackTrace();
           return null;
        }
     }
  }

.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test-results-upload</groupId>
<artifactId>test-results-upload</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<properties>
    <spring.boot>1.1.5.RELEASE</spring.boot>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>${spring.boot}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <version>${spring.boot}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>${spring.boot}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>${spring.boot}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
        <version>${spring.boot}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
        <version>${spring.boot}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-rest-webmvc</artifactId>
        <version>2.1.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
        <version>${spring.boot}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>${spring.boot}</version>
        <type>maven-plugin</type>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.4.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.4.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.4.2</version>
    </dependency>
</dependencies>

<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <warSourceDirectory>WebContent</warSourceDirectory>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
    </plugins>
</build>

解决方案

自从首次提出此问题以来已经有一段时间了,分享了我为将Spring boot application打包为war打包部署在Tomcat服务器上所做的工作(External),使用Maven:

  1. 可部署Servlet 3.0 API容器的war文件(意味着它可以在Tomcat 7.0.x及更高版本上正确部署)
    • 环境设置:
      • 需要安装Tomcat 7.0.x版本,因为可以从 Tomcat 7.0获得对Servlet 3.0 API的支持. x 起.
      • Java 1.8(确保在相同的Java版本上进行构建(JDK8)并运行(JRE8 Tomcat JVM目标)).
        • 注意:Java 1.7也可以,只需确保编译并在相同版本上运行即可.
    • 代码:
      • 引导应用程序类更改:请遵循扩展SpringBootServletInitializer
      • Maven构建文件pom.xml更改:
        • <artifactId>spring-boot-starter-tomcat</artifactId>的范围更改为<scope>provided</scope>
        • 最后,我们需要将其捆绑为war文件,以便更改包装<packaging>war</packaging>

I have a spring-boot application and now that I want to deploy the application on a dedicated tomcat server and not using the embedded tomcat. Both ways on deploying I'm not getting any error.

I have given the scope=provided for the spring-boot-starter-tomcat dependency.

When I run the application with embedded tomcat (with appropriate changes made), its working perfectly on hitting the link http://localhost:8080/testGET. But when I ran on the dedicated tomcat with scope=provided, on hitting the link http://localhost:8080/test-results-upload-1.0/testGET or http://localhost:8080/testGET I'm getting the response mapped to /error.

Please help me with this. Not able to understand mistake I'm doing..

Thanks in advance.

 @EnableAutoConfiguration
 @Configuration
 @EnableWebMvc
 @ComponentScan("com............controller")
 @Import(SpringMongoConfig.class)
 public class BootStrap extends SpringBootServletInitializer {

    public static void main(String[] args) {
       SpringApplication.run(BootStrap.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(
           SpringApplicationBuilder application) {
       return application.sources(BootStrap.class);
    }
}

.

  @Controller
  public class Controller {

     @Autowired(required = true)
     private IRepository config;

     @RequestMapping(value = "/testGET", method = RequestMethod.GET)
     public String testGet(HttpServletResponse response) {
        try {
           response.sendError(HttpStatus.OK.value());
           return "Application working perfectly !";
        } catch (IOException e) {
           e.printStackTrace();
           return null;
        }
     }
  }

.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test-results-upload</groupId>
<artifactId>test-results-upload</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<properties>
    <spring.boot>1.1.5.RELEASE</spring.boot>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>${spring.boot}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <version>${spring.boot}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>${spring.boot}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>${spring.boot}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
        <version>${spring.boot}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
        <version>${spring.boot}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-rest-webmvc</artifactId>
        <version>2.1.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
        <version>${spring.boot}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>${spring.boot}</version>
        <type>maven-plugin</type>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.4.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.4.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.4.2</version>
    </dependency>
</dependencies>

<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <warSourceDirectory>WebContent</warSourceDirectory>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
    </plugins>
</build>

解决方案

It has been a while since this question was first asked, sharing what I did to get a Spring boot application packaged as war deployed on Tomcat server (External), using Maven:

  1. Deployable war file for Servlet 3.0 API Container (meaning it can be deployed correctly on Tomcat 7.0.x onwards)
    • Environment Setup :
      • Need to have Tomcat 7.0.x version installed, as Support for Servlet 3.0 API is available from Tomcat 7.0.x onwards.
      • Java 1.8 (Make sure to build (JDK8) and run(JRE8 Tomcat JVM target) on the same Java versions).
        • Note:Java 1.7 would also do, just make sure to compile and run on same versions.
    • Code:
      • Boot Application class changes: follow the instructions of extending SpringBootServletInitializer
      • Maven build file pom.xml changes:
        • Change of scope for <artifactId>spring-boot-starter-tomcat</artifactId> to <scope>provided</scope>
        • Finally we need to bundle it as an war file so change of packaging <packaging>war</packaging>

这篇关于在专用的tomcat上将spring-boot应用程序作为war文件部署不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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