首页 文章

使用Spring Boot的Spring MVC不能与Eclipse的Tomcat服务器一起使用

提问于
浏览
3

我尝试遵循Spring入门指南“使用Spring MVC提供Web内容”,除了Maven之外还使用Spring Boot和Gradle . 我将Gradle插件安装到Eclipse .

我想在Eclipse中使用Tomcat服务器运行应用程序,因为我还遵循“将Spring Boot JAR应用程序转换为WAR”指南并更改了指南中提到的“build.gradle”文件 . 基本上,我添加了行“apply plugin:'eclipse-wtp'”,“apply plugin:'war'”,configurations 和providedRuntime(“org.springframework.boot:spring-boot-starter-tomcat”);并将jar设置更改为war设置 . 这是build.gradle文件:

buildscript {
repositories {
    maven { url "http://repo.spring.io/libs-milestone" }
    mavenLocal()
}
dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.2.RELEASE")
}
}

apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'

eclipse.project {
  natures 'org.springsource.ide.eclipse.gradle.core.nature'
}

war {
    baseName = 'gs-serving-web-content'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
    maven { url "http://repo.spring.io/libs-milestone" }
}

configurations {
    providedRuntime
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    testCompile("junit:junit")
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
}

task wrapper(type: Wrapper) {
    gradleVersion = '1.11'
}

我还提到了HelloWebXml类 . 这是 class :

package hello;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

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

除了这些,我还需要更改pom.xml,因为它抱怨Java SE 7的功能 . 在pom.xml中添加了以下行:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>1.7</source>
            <target>1.7</target>
            <showWarnings>true</showWarnings>
            <compilerVersion>1.7</compilerVersion>
            <fork>true</fork>
        </configuration>
</plugin>

其余部分与入门指南相同 .

对于建筑,我跑

gradlew eclipseWtp
gradlew clean build

命令 . 在此之后,war文件在项目的build / libs文件夹中创建 . 如果我将war文件复制到我的本地Tomcat服务器并启动服务器,一切都按预期工作 .

如果我将项目拖到“服务器”选项卡下的Tomcat服务器(使用相同的本地Tomcat创建)并运行服务器,则会引发ClassCastException并出现投诉:“ Cannot cast org.springframework.web.SpringServletContainerInitializer to javax.servlet.ServletContainerInitializer ” .

我在两个部署位置检查了项目的文件夹结构 . 在本地(非Eclipse)部署位置,服务器启动后,将按预期创建具有war文件名称的文件夹 . 在WEB-INF文件夹中,有一个 lib-provided 目录 . 我检查了Eclipse的Tomcat的部署位置,它没有找到解决方案 .

我已经在使用Spring MVC了,我知道如何使用web.xml创建MVC项目,但我是Spring Boot的新手 . Eclipse的Tomcat服务器运行我之前的Spring MVC项目 . 所以问题在于Spring Boot项目 .

我检查了几个相关问题,但它们和我的不一样 . 我无法找到解决问题的方法 .

我在这里错过了什么?

提前致谢 .

3 回答

  • 4

    我想你正在与 servlet-api JAR发生冲突 . 如果使用嵌入式Tomcat(创建JAR)进行开发,则需要在 compile 范围内的类路径中包含 servlet-api JAR . 在部署到现有Tomcat安装(创建WAR)时,必须在 provided 范围内声明 servlet-api jar,否则它将在 web-inf/lib 中结束并导致与容器提供的版本冲突 .

    以下是示例Spring Boot WAR项目中的POM:https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-traditional/pom.xml

    请注意 spring-boot-starter-tomcat 如何使用 provided 范围 .

  • 0

    Spring Boot进行了一些特殊处理,以允许可嵌入或在容器中运行的可执行WAR . 必须特别注意 provided 范围内的依赖关系,因为这些可能与容器冲突 . 细节描述in the Spring Boot reference guide . 通过构建的WAR而不是通过Eclipse部署时,构建工作的原因是Eclipse WTP没有完全匹配 .

    我专门为此目的开发了'maven'模块,并使用标签将 provided 条目切换(某些)到 runtime .

  • 0

    @meribald你可以在那个配置你的pom.xml:

    <profiles>
            <profile>
                <id>local</id>
                <activation>
                    <activeByDefault>true</activeByDefault>
                </activation>
                <properties>
                    <packaging-profile>jar</packaging-profile>
                </properties>
                <dependencies>
                    <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-tomcat</artifactId>
                    </dependency>
                </dependencies>
            </profile>
            <profile>
                <id>prod</id>
                <properties>
                    <packaging-profile>war</packaging-profile>
                </properties>
                <dependencies>
                    <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-tomcat</artifactId>
                        <scope>provided</scope>
                    </dependency>
                </dependencies>
            </profile>
        </profiles>
    

    在你的pom.xml的顶部

    <packaging>${packaging-profile}</packaging>
    

    因此,在开发时,将应用程序作为Java应用程序运行,maven将生成一个jar文件 . 如果要打包war文件以在服务器上部署,则可以在maven命令行上运行

    mvn -Pprod package
    

    或者在Eclipse中运行>> Maven Build ...,并在目标字段上放置“package”,在profiles字段上放置“prod”(不带引号) .

相关问题