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;
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:
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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With