首页 文章

Spring Boot war文件适用于嵌入式,但不适用于独立的Tomcat

提问于
浏览
6

我想从 Spring Boot application, which I can deploy to a standalone Tomcat container, not using the embedded one. 创建一个war文件

我可以创建war文件并使用 "java -jar pdfjs-annotator.war" 自己运行它,它工作正常 .

我使用 gradle bootRepackage (使用Gradle,Tomcat7,Java 1.7)构建了应用程序 .

但是,当我将war文件部署到独立的Tomcat并启动它时,应用程序似乎根据日志启动时没有错误,但我无法访问任何资源,也无法控制器URL工作 .

例如,我的index.html是src / main / resources / static / index.html下的静态html页面,我通常可以通过localhost:8080 / index.html调用,但是当部署到独立的Tomcat时,页面会执行没有通过相同的URL传递(它在WEB-INF / classes / static / index.html的war文件中) . 而且任何类型的控制器映射似乎都不起作用 . 我收到了404错误 .

build.gradle:

buildscript {
    ext {
        springBootVersion = '1.2.3.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
        classpath("io.spring.gradle:dependency-management-plugin:0.5.0.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'idea'
apply plugin: 'spring-boot' 
apply plugin: 'io.spring.dependency-management' 
apply plugin: 'war'


war {
    baseName = 'pdfjs-annotator'
    version = '1.0.0-SNAPSHOT'
}

allprojects {
    apply plugin: 'java'
    sourceCompatibility = 1.6
    targetCompatibility = 1.6
}

repositories {
    mavenCentral()
}

configurations {
    providedRuntime
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("org.springframework.boot:spring-boot-starter-data-rest")
    compile("org.springframework.boot:spring-boot-starter-web")
    runtime("mysql:mysql-connector-java")
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
    testCompile("org.springframework.boot:spring-boot-starter-test")
    compile ('org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final')
}


eclipse {
    classpath {
         containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
         containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7'
    }
}

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

我的主要 application 课程:

@EnableJpaRepositories
@SpringBootApplication
public class PdfAnnotator extends SpringBootServletInitializer {

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

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

}

当我看到爆炸的战争时,我确实看到以下 META-INF/MANIFEST.MF

Manifest-Version: 1.0
Start-Class: com.mypackage.pdfcomment.PdfAnnotator
Spring-Boot-Version: 1.2.3.RELEASE
Main-Class: org.springframework.boot.loader.WarLauncher

gradle构建过程通常会生成两个war工件,一个名为.war,一个名为.war.original - .war是一个保存正确的MANIFEST.MF条目,它是我用来部署到独立Tomcat的那个 .

少了什么东西?我已经在SO上检查了其他问题:

还有Spring Boot文档,但是找不到什么是错的 .

*** === Update === ***

我安装了一个全新的Tomcat7,在那里部署了war文件,一切正常 . 似乎是我运行的Tomcat实例/配置的一些问题 . 不知道究竟是什么问题,但我不打算进一步检查它,因为现在使用新的Tomcat工作正常 .

2 回答

  • 0

    我遇到了这个问题 . 这是因为我的应用程序是使用Java 1.8构建的,但Tomcat是使用1.7运行的 . 一切似乎都正确部署,没有错误 . 我只是得到了404尝试打我的服务,否则嵌入式tomcat服务器就好了 . 花了我很多时间来解决它,但这只是在Tomcat安装上升级java的一个例子 .

  • 1

    该项目实际上是正确设置的,事实证明问题出在我正在运行的Tomcat实例上 . 配置或 jar 可能有问题 . 实例已经存在了很长时间,也许随着时间的推移,事情变得混乱 .

    现在安装了一个全新的Tomcat7实例,战争就可以了 .

相关问题