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

ran*_*er1 10 war amazon-web-services spring-boot amazon-elastic-beanstalk

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

spring.mvc.view.prefix: /
spring.mvc.view.suffix: .jsp
server.port=5000
Run Code Online (Sandbox Code Playgroud)

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

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

在此输入图像描述

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

在此输入图像描述

Whitelabel错误页面此应用程序没有/ error的显式映射,因此您将此视为回退.

Thu Dec 20 16:30:33 UTC 2018出现意外错误(type = Not Found,status = 404)./welcome.jsp

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

- - 编辑

根据要求,这是根java类:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的项目的结构与突出显示的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>
Run Code Online (Sandbox Code Playgroud)

并且UserController该类包含:

...

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

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

    return "welcome";
}

...    
Run Code Online (Sandbox Code Playgroud)

我在welcome方法中添加了一些日志,我发现它运行正常.此外,在日志文件中,我可以看到以下条目:

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

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

---编辑:

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

Dip*_*oke 4

解决方案一:

如果您想要在可执行 Jars 中使用 Spring Boot 和 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
Run Code Online (Sandbox Code Playgroud)

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

java -jar <your jar name>

 for your project you can below command

   java -jar  auth-1.3.5.RELEASE.jar
Run Code Online (Sandbox Code Playgroud)

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

解决方案2:

JSP 限制

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

对于 Jetty 和 Tomcat,如果您使用 war 包装,它应该可以工作。可执行的 war 在使用 java -jar 启动时可以工作,并且还可以部署到任何标准容器。使用可执行 jar 时不支持 JSP。Undertow 不支持 JSP。创建自定义 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/
Run Code Online (Sandbox Code Playgroud)

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

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