亚马逊网络服务-尝试访问Amazon Elastic Bean Stalk上的Spring Boot应用程序时出现错误404

我开发了一个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:5000(http),并且可以在浏览器中的http://localhost:5000/welcome下访问该应用程序.

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

enter image description here

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

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

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

– – 编辑

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

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

enter image description here

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

enter image description here

我的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类包含:

...

@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个小时后,我开始质疑自己的生活选择,而且我想知道为什么有人会使用这种愚蠢的框架,因为它无法正常工作.

—编辑:

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

最佳答案
解决方案1:

如果要在可执行Jar中使用JSP进行Spring Boot

请记住,我们最终会将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

解决方案2:

JSP局限性

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

对于Jetty和Tomcat,如果使用战争包装,它应该可以工作.与java -jar一起启动时,可执行的war将起作用,并且还将可部署到任何标准容器中.使用可执行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/

您可以在浏览器中查看结果.

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

转载注明原文:亚马逊网络服务-尝试访问Amazon Elastic Bean Stalk上的Spring Boot应用程序时出现错误404 - 代码日志