Spring Boot War file gets 404 on ec2

2019-07-21 04:53发布

问题:

I put together a simple Eclipse Spring Boot Rest program. Just a couple of endpoints that return some strings. I am building it with the Eclipse Gradle plug-in. No problems, it runs fine in the Eclipse provided Tomcat instance. I also ran it on a native windows Tomcat. I deployed the .war file using the tomcat web application manager using the deploy feature. Runs fine. I then deployed an ec2 ubuntu instance and again used the tomcat web application manager and upload the .war file. The rest program is correctly displayed in the applications window but it will not run. I used the following URL,

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

I keep getting 404's. I am pretty sure the tomcat deployment and ec2 environment appears correct because I can run the tomcat7-examples in the ec2 instance with no issues. Any help would be appreciated.

AwsElbRestServerAApplication.java file:

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 file:

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 file:

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 file:

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

回答1:

I've answered a similar question here.

You are running your external tomcat under a Java 1.7 JRE, while having compiled your code against 1.8.

Strangely, there is no error, and the app appears in the manager app, but then you get a 404 when you're trying to access it.

One way to confirm this is to look at your tomcat log output, you won't see the Spring Boot banner.

To solve this you can compile your code against Java 1.7.