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

时间:2015-10-14 20:56:48

标签: spring tomcat spring-boot

我正在尝试从Spring-boot项目创建一个WAR文件,该文件允许用户使用CREATE proc [Add_Student]( @stud_id varchar(5), @fname varchar(25), @lname varchar(25), @balance decimal = 0 ) AS BEGIN BEGIN TRANSACTION INSERT INTO MyStudent(Student_ID, Fname, Lname, Acct_Bal) VAULES (@stud_id, @fname, @lname, @balance) IF @@ERROR <> 0 BEGIN Rollback Transaction Raiserror ('Unable to insert record.',16,1) return -1 END ELSE BEGIN Commit Transaction Print 'Record Added Successfully!' END END 运行该文件,并使用tomcat部署该文件。

现在,我有这样的配置:

的pom.xml

java -jar filename

Application.java

<?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!"; } } 在命令行中运行应用程序,但是当我在tomcat中部署war时,在访问/ page时出现404错误页面。 (部署应用程序时不会显示错误。)

我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

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

如果您无法升级到Tomcat 7或8,那么仍然可以让您的应用运行。但是,它需要更多的努力。作为described in the documentation,您需要在org.springframework.boot:spring-boot-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>