我在 Amazon Elastic Bean Stalk 上尝试访问我的 spring 启动应用程序时收到错误 404

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

我开发了一个 spring boot 应用程序,并将以下条目放入 src/main/resources/application.properties

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

现在,当我在本地启动它 (mvn clean spring-boot:run) 时,我得到了输出 Tomcat started on port(s): 5000 (http) 并且可以在 http://localhost:5000/welcome 下的浏览器中访问该应用程序。

我在Amazon Elastic Bean Stalk中创建了一个Java实例,我已经上传了war,我还在EC2实例上相应的安全组中打开了端口5000:

但是当我现在转到 http://my-aws-ebs-instance.com/welcome:5000 时,我收到以下消息:

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

为什么哦为什么会这样?我忘记配置了什么?

----编辑

根据要求,这是根 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);
    }
}

这也是我项目的结构,突出显示了 welcome.jsp 页:

当我解压缩生成的 war 文件时,这是我硬盘上的文件结构:

我的 pom.xml 文件:

<?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>

并且 UserController class 包含:

...

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

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

    return "welcome";
}

...    

我在 welcome 方法中添加了一些日志,我看到它是正确的 运行。此外,在日志文件中我可以看到以下条目:

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

所以我不知道为什么这个东西不起作用。在连续尝试了 11 个小时让它工作之后,我开始质疑我的人生选择,而且我也想知道为什么有人会使用这样一个愚蠢的框架,因为它不起作用 ootb。

--- 编辑:

我已将简化代码上传至 github https://github.com/nalogowiec/springbootProblem

因为你的应用程序在端口 5000 上,它可以在该端口上访问,而不是默认的 http 端口 80。

使用

访问它
http://my-aws-ebs-instance.com:5000/welcome

或在 AWS 中创建端口转发符文,以便将去往端口 80 的流量推送到应用程序服务器的端口 5000。

如果您查看 Spring 引导文档,很明显您使用了错误的目录结构。

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-spring-mvc

By default, Spring Boot serves static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath or from the root of the ServletContext ... Do not use the src/main/webapp directory if your application is packaged as a jar. Although this directory is a common standard, it works only with war packaging, and it is silently ignored by most build tools if you generate a jar.

解决方案一:

如果您想要 Spring 在可执行 Jar 中使用 JSP 启动

请记住,我们最终会将 JSP 模板放在 src/main/resources/META-INF/resources/WEB-INF/jsp/

注: 为 application.properties

中的 JSP 文件定义模板前缀和后缀
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

然后你可以 运行 jar 文件使用下面的命令:

java -jar <your jar name>

 for your project you can below command

   java -jar  auth-1.3.5.RELEASE.jar

更多参考:https://dzone.com/articles/spring-boot-with-jsps-in-executable-jars-1

方案二:

JSP 限制

当运行使用嵌入式 servlet 容器(并打包为可执行存档)的 Spring 引导应用程序时,JSP 支持存在一些限制。

使用 Jetty 和 Tomcat,如果您使用 war 打包应该可以。可执行文件 war 将在使用 java -jar 启动时运行,并且还可以部署到任何标准容器。使用可执行 jar 时不支持 JSPs。 Undertow 不支持 JSPs。 创建自定义 error.jsp 页面不会覆盖错误处理的默认视图。应改用自定义错误页面。

我已经将你的 GitHub 项目克隆到 运行 项目(如果你按照以下步骤操作,你的问题肯定会得到解决)

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/

您可以在浏览器中看到结果。

更多参考:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-jsp-limitations

很好的解释@dipak-thoke。

如果有人自动执行部署过程(在我的例子中,是通过 CodeBuild 和 CodeDeploy),只需添加,您可以创建 Procfile 并部署 war。我已将 Procfile 添加到项目的根目录中并将其添加为工件。 希望这有助于寻找相同用例的人:)

过程文件:

web: java -jar <your_war_file>.war 

这是我的 CodeBuild 构建规范的样子:

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