2

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'
}
ci_
  • 8,594
  • 10
  • 39
  • 63
skmansfield
  • 1,413
  • 3
  • 19
  • 41
  • I've answered something similar only yesterday http://stackoverflow.com/questions/30908579/spring-boot-war-file-deploy-on-tomcat/30914320#30914320 any chance it's the same issue? – ci_ Jun 19 '15 at 15:05
  • Yes, you answered it. I just changed my Eclipse jre to 1.7 and it worked! By default Eclipse selects Java 1.8 in the project creation menu but I am using Tomcat7. Thanks a bunch and great catch! – skmansfield Jun 19 '15 at 18:18
  • Hey ci_ can you answer your question with an answer so I can give you credit for the correct answer? – skmansfield Jun 20 '15 at 22:02
  • I've posted an answer. – ci_ Jun 21 '15 at 10:18

1 Answers1

14

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.

Community
  • 1
  • 1
ci_
  • 8,594
  • 10
  • 39
  • 63
  • 1
    This was exactly the problem. I was running spring boot with java jre 1.8 but using Tomcat7. I changed to java 1.7 and it worked perfectly. Thanks for the great answer. – skmansfield Jun 21 '15 at 14:47