Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I'm getting error 404 while trying to access my spring boot app on Amazon Elastic Bean Stalk

I developed a spring boot application and I've put the following entries in src/main/resources/application.properties:

spring.mvc.view.prefix: /
spring.mvc.view.suffix: .jsp
server.port=5000

Now when I start it (mvn clean spring-boot:run) locally, I'm getting the output Tomcat started on port(s): 5000 (http) and the app is accessible in the browser under http://localhost:5000/welcome .

I created a Java instance in Amazon Elastic Bean Stalk, I've uploaded war, I even opened the port 5000 in the corresponding Security Group on EC2 instance:

enter image description here

but when I now go to http://my-aws-ebs-instance.com/welcome:5000, I'm getting the following message:

enter image description here

Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Dec 20 16:30:33 UTC 2018 There was an unexpected error (type=Not Found, status=404). /welcome.jsp

Why oh why does it happen like this? What did I forget to configure?

----EDIT

as requested, here's the root java class:

package com.hellokoding.auth;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

@SpringBootApplication
public class WebApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(WebApplication.class);
    }

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

Here is also the structure of my project with highlighted welcome.jsp page:

enter image description here

When I unzip the generated war file, this is the file structure on my hard drive:

enter image description here

My pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>auth</artifactId>
    <name>auth</name>
    <description>my descr</description>
    <packaging>war</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.7</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

and the UserController class contains:

...

@Controller
@Scope("session")
public class UserController {

@RequestMapping(value = {"/", "/welcome"}, method = RequestMethod.GET)
public String welcome(Model model) {

    return "welcome";
}

...    

I added some logs inside the welcome method and I see it is running correctly. Also, in log files I can see the following entry:

Mapped "{[/ || /welcome],methods=[GET]}" onto public java.lang.String com.hellokoding.auth.web.UserController.welcome(org.springframework.ui.Model)

so I have no idea why this thing does not work. After trying for 11 hours straight to make it work I'm questioning my life choices, and also I'm wondering why anyone would ever use such a stupid framework since it doesn't work ootb.

--- edit:

I've uploaded a simplified code to github https://github.com/nalogowiec/springbootProblem

like image 948
randomuser1 Avatar asked Dec 20 '18 16:12

randomuser1


2 Answers

Nice explanation @dipak-thoke.

Just to add if anyone automating the deployment process (In my case, it was through CodeBuild And CodeDeploy), you can create Procfile and deploy the war. I have added Procfile into the root directory of the project and added it as an artifact. Hope this helps someone looking for same usage case :)

ProcFile:

web: java -jar <your_war_file>.war 

This is how my CodeBuild Buildspec looks like:

version: 0.2

phases:

  build:
    commands:
      # - command
      - ./gradlew bootWar
  post_build:
    commands:
      # - command
      - echo Build must be completed
      - mv build/libs/*.war <WarFileName>.war

artifacts:
  files:
    # - location
     - <WarFileName>.war
     - Procfile
  #name: $(date +%Y-%m-%d)
  #discard-paths: yes
  #base-directory: location
#cache:
  #paths:
    # - paths
like image 137
Adarsh Avatar answered Oct 04 '22 03:10

Adarsh


Solution 1:

If you want Spring Boot With JSPs in Executable Jars

Keep in mind that we will ultimately place the JSP templates under src/main/resources/META-INF/resources/WEB-INF/jsp/

Note : define the template prefix and suffix for our JSP files in application.properties

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

Then your can run jar file using below command :

java -jar <your jar name>

 for your project you can below command

   java -jar  auth-1.3.5.RELEASE.jar

For More reference : https://dzone.com/articles/spring-boot-with-jsps-in-executable-jars-1

Solution 2:

JSP Limitations

When running a Spring Boot application that uses an embedded servlet container (and is packaged as an executable archive), there are some limitations in the JSP support.

With Jetty and Tomcat, it should work if you use war packaging. An executable war will work when launched with java -jar, and will also be deployable to any standard container. JSPs are not supported when using an executable jar. Undertow does not support JSPs. Creating a custom error.jsp page does not override the default view for error handling. Custom error pages should be used instead.

I have clone your GitHub project able to run project(if you follow below steps your problem will get solve definitely)

Step To run your project :

Step 1 : Create war package of your project

Step 2 : Run your war package using below command 

    java -jar <your war file name> 

    i.e for your project command should be like :

      java -jar  auth-1.3.5.RELEASE.war

Step 3 : Hit the URL  http://localhost:5000/

You can see the result in browser.

More reference : https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-jsp-limitations

like image 28
Dipak Thoke Avatar answered Oct 04 '22 03:10

Dipak Thoke