Spring 引导 War 文件在 ec2 上获得 404

Spring Boot War file gets 404 on ec2

我整理了一个简单的 Eclipse Spring Boot Rest 程序。只是 return 一些字符串的几个端点。我正在使用 Eclipse Gradle 插件构建它。没问题,在 Eclipse 提供的 Tomcat 实例中 运行 没问题。我也 运行 它在原生 windows Tomcat 上。我使用部署功能使用 tomcat Web 应用程序管理器部署了 .war 文件。运行良好。然后我部署了一个 ec2 ubuntu 实例并再次使用 tomcat Web 应用程序管理器并上传了 .war 文件。其余程序在应用程序 window 中正确显示,但不会 运行。我使用了以下 URL,

http://ec2-IP-Address/demo-0.0.1-SNAPSHOT/hello

我不断收到 404。我很确定 tomcat 部署和 ec2 环境看起来是正确的,因为我可以 运行 ec2 实例中的 tomcat7-examples 没有问题。任何帮助将不胜感激。

AwsElbRestServerAApplication.java 文件:

package demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@SpringBootApplication
public class AwsElbRestServerAApplication {

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

ServletInitializer.java 文件:

package demo

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

public class ServletInitializer extends SpringBootServletInitializer {

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

RestController.java 文件:

package demo

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class RestController {

    @RequestMapping("/hello")
    public @ResponseBody String hello() {

        return "Greetings from Server A";
    }

    @RequestMapping("/heartbeat")
    public @ResponseBody String heartbeat() {

        return "Server A is still running";
    }
}

build.gradle 文件:

buildscript {
    ext {
        springBootVersion = '1.2.4.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle
        plugin:${springBootVersion}") 
        classpath("io.spring.gradle:dependency-management
        Plugin:0.5.1.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 = 'demo'
    version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

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

configurations {
    providedRuntime
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-actuator")
    compile("org.springframework.boot:spring-boot-starter-remote-shell")
    compile("org.springframework.boot:spring-boot-starter-web")
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
    testCompile("org.springframework.boot:spring-boot-starter-test") 
}

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.8'
    }
}

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

我回答过类似的问题

您是 运行 您的外部 tomcat 在 Java 1.7 JRE 下,同时针对 1.8 编译了您的代码。

奇怪的是,没有任何错误,应用程序出现在管理器应用程序中,但是当您尝试访问它时却收到 404。

确认这一点的一种方法是查看您的 tomcat 日志输出,您不会看到 Spring 启动横幅。

要解决此问题,您可以针对 Java 1.7.

编译代码