基于spring boot项目构建可部署的WAR

基于spring boot项目构建可部署的WAR,spring,tomcat,spring-boot,Spring,Tomcat,Spring Boot,我正在尝试从一个Spring启动项目创建一个WAR文件,它允许用户使用java-jarfilename运行该文件,并使用tomcat部署该文件 现在,我有以下配置: pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xs

我正在尝试从一个Spring启动项目创建一个WAR文件,它允许用户使用
java-jarfilename
运行该文件,并使用tomcat部署该文件

现在,我有以下配置:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>org.springframework</groupId>
    <artifactId>springapp</artifactId>
    <version>1.0.0</version>
    <packaging>war</packaging>

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

    <properties>
        <java.version>1.7</java.version>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
HelloController.java

package org.hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

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

}
package org.hello;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
public class HelloController {

    @RequestMapping("/")
    public String index() {
        return "Greetings from Spring Boot!";
    }

}
通过这种配置,我可以在命令行中使用
java-jarfilename
运行应用程序,但是当我在tomcat中部署war时,当我访问/page时,会得到一个404错误页面。(部署应用程序时不显示错误)


这里缺少什么?

默认情况下,Spring Boot要求您的容器支持Servlet 3.0或更高版本。Tomcat6只支持Servlet2.5。最简单的选择是升级到Tomcat 7或8

如果你不能升级到Tomcat 7或8,那么仍然可以让你的应用程序运行。然而,这需要更多的努力。同样,您需要添加对
org.springframework.boot:springboot legacy
的依赖项,并添加具有以下内容的
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>demo.Application</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener</listener-class>
    </listener>

    <filter>
        <filter-name>metricFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>metricFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextAttribute</param-name>
            <param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

上下文配置位置
演示应用程序
org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener
计量过滤器
org.springframework.web.filter.DelegatingFilterProxy
计量过滤器
/*
appServlet
org.springframework.web.servlet.DispatcherServlet
上下文属性
org.springframework.web.context.WebApplicationContext.ROOT
1.
appServlet
/

当应用程序部署到Tomcat时,您用来访问该应用程序的完整URL是什么?war文件的名称是什么?完整URL是
http://server:8080/springapp-1.0.0
和war文件是
springapp-1.0.0.war
。您说没有显示错误。还展示了什么?您应该在Tomcat的日志中看到输出,显示正在部署的war和正在启动的应用程序–Spring Boot的横幅等。在文件
/var/log/tomcat6/catalina.out
中,部署后唯一的一行显示是:
10月15日,2015 11:38:34 AM org.apache.catalina.startup.HostConfig deployWAR//INFO:Deploying web application archive springapp-1.0.0.war
我应该将这个web.xml文件放在我的项目树中的什么位置?可能是/src/main/resources?