4

I'm very new at both AWS and Spring Framework.

I've built a simple web program following the getting started guide in Spring website. Also, I already made my Elastic Beanstalk application and environment running Tomcat 8 and Java 8. What I'm trying to is;

  1. build this simple program using Gradle and produce an executable jar( or war).
  2. upload this executable jar(or war) from 'upload and deploy' button in my Elastic Beanstalk dashboard.

I'm using IntelliJ now and my program works well if I double-clicked the produced executable jar. However, after I uploaded this jar to my Elastic Beanstalk, this program just produces HTTP 404 error.

What I don't understand is, if I use Gradle 'build' task or 'bootRun' task, the produced executable jar file works fine. However, if I build a war file through Gradle 'war' task and put it on my local Tomcat, the server produces HTTP 404 error same as AWS.

This is my build.gradle file.

    buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.7.RELEASE")
        classpath("org.apache.tomcat:tomcat-catalina:8.0.28")
    }
}

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

jar {
    baseName = 'gs-authenticating-ldap'
    version =  '0.1.0'
}

war {
    baseName = 'gs-authenticating-ldap'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

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

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

Besides, my program only has two Java classes: src/main/java/hello/HomeController.java and src/main/java/hello/Application.java

src/main/java/hello/HomeController.java is:

package hello;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

    @RequestMapping("/")
    public String index() {
        return "Welcome to the home page!";
    }
}

src/main/java/hello/Application.java is:

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

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

}

As a summary:

  • build 'bootRun' -> browse "localhost:8080" : Works fine.
  • build 'build' -> produce 'xxx.jar(executable)' -> double click to execute this jar -> browse "localhost:8080" : Works fine
  • build 'build' -> produce 'xxx.jar(executable)' -> upload to AWS -> browse "xx.elasticbeanstalk.com" : HTTP 404 error
  • build 'war' -> produce 'xxx.war' -> run this war on local Tomcat using IntelliJ -> browse "localhost:8080" : HTTP 404 error
  • build 'war' -> produce 'xxx.war' -> upload to AWS -> browse "xx.elasticbeanstalk.com" : HTTP 404 error

What I want to do is 1) develop on IntelliJ, 2) build it on Gradle, 3) test it on locally, and 4) upload it on AWS to run. Please, help me.

If you have any further explanation or information, please let me know. Thanks!

2
  • Did you resolve this? I have some ideas based on my own testing. Commented Feb 16, 2016 at 21:39
  • 1
    Beanstalk uses Nginx as reverse proxy to your service, and defaults to port 5000 on the backend. Try setting SERVER_PORT to 5000 in the Beanstalk configuration. Anyway, the error should be 503... Commented Aug 23, 2017 at 9:52

2 Answers 2

6

I had a similar issue, what you need to do is first make your Application class extends SpringBootServletInitializer:

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

}

It will initialize your app using Spring Framework’s Servlet support (Tomcat will be able to register the servlet and start the app).

Then you have to tell Gradle that the environment runtime will be provided by the application server. To do that add the following line under dependencies:

dependencies {
    // …
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
    // …
}

Build a new war (build war) and deploy it.

For more information here is the documentation on how to deploy a Spring Boot app as a war: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html

Sign up to request clarification or add additional context in comments.

1 Comment

Big thanks for this answer, it solved my problem too... though I was using maven, so I had to add this to my dependencies: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
3

As a followup, AWS has published an updated guide that shows how to deploy a .jar file and that uses the embedded TomCat server.

https://aws.amazon.com/blogs/devops/deploying-a-spring-boot-application-on-aws-using-aws-elastic-beanstalk/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.