尝试访问Amazon Elastic Bean Stalk上的Spring Boot应用程序时出现错误404 [英] I'm getting error 404 while trying to access my spring boot app on Amazon Elastic Bean Stalk

查看:111
本文介绍了尝试访问Amazon Elastic Bean Stalk上的Spring Boot应用程序时出现错误404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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 .

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

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:

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

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

Whitelabel错误页面此应用程序没有针对 /错误,因此您将其视为备用.

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

2018年12月20日星期四20:30:33 UTC发生意外错误(type = Not 找到,状态= 404)./welcome.jsp

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?

----编辑

根据要求,这是根Java类:

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);
    }
}

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

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

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

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

我的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方法中添加了一些日志,并且看到它运行正常.另外,在日志文件中,我可以看到以下条目:

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)

所以我不知道为什么这个东西不起作用.在连续尝试了11个小时后,我开始质疑自己的生活选择,而且我想知道为什么有人会使用这种愚蠢的框架,因为它无法正常工作.

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.

---

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

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

推荐答案

解决方案1:

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

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

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

注意: 在application.properties中为我们的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

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

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

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

解决方案2:

JSP限制

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

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.

对于Jetty和Tomcat,如果使用战争包装,它应该可以工作.与java -jar一起启动时,可执行的war将起作用,并且还将可部署到任何标准容器中.使用可执行jar时,不支持JSP. Undertow不支持JSP. 创建定制的error.jsp页面不会覆盖默认视图以进行错误处理.应该改用自定义错误页面.

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.

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

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.

更多参考资料: 查看全文

相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆