将 Spring Boot(带有 JSP)部署到 Elastic Beanstalk

Bip*_*ala 5 spring jsp tomcat spring-boot amazon-elastic-beanstalk

我正在尝试将 Spring Boot 项目部署到 Amazon Elastic Beanstalk。我已经测试过,如果我使用默认的 Thymeleaf 配置,没有问题,但是当我切换到基于 JSP 的设置时,我得到 404,因为它找不到 JSP(位于 src/main/webapp/WEB-INF/jsp 中)

我尝试部署示例(spring-boot-sample-tomcat-jsp),并发现当我运行提供的测试时,这也给了我一个 404 错误。

以下是我通常如何配置 Spring Boot 项目以允许使用 JSP。

  1. 将 Jasper 和 JSTL 添加到 pom.xml

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
    
    Run Code Online (Sandbox Code Playgroud)
  2. 覆盖默认视图解析器配置

    @Configuration
    @EnableWebMvc
    public class MvcConfiguration extends WebMvcConfigurerAdapter
    {
    
        @Bean
        public ViewResolver getViewResolver(){
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/jsp/");
            resolver.setSuffix(".jsp");
    
            return resolver;
        }
    
        @Override
        public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){
            configurer.enable();
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 为 JSP 创建文件夹(src/main/webapp/WEB-INF/jsp)

现在,此方法可以正常工作,直到我部署到 Elastic Beanstalk,我发现无论我创建 jar 并使用 Java(在 Elastic Beanstalk 中)还是创建战争并使用 Tomcat(在 eb 中),当任何控制器尝试时,我都会收到 404返回 WEB-INF/jsp 文件夹中的视图。

不推荐使用上面的方法来切换到JSP吗?有没有更好的方法来配置 Spring Boot 以使用 Tomcat/Jasper/JSP?

我已经尝试了github上的 Spring Boot 示例中提供的方法

但有趣的是,如果我运行提供的测试,我会得到相同的 404。

任何帮助将不胜感激。如果有更好的方法来部署利用 JSP 的 Spring Boot 项目,我很乐意切换,但目前我似乎已经将自己配置到了一个角落。

谢谢!

Kar*_*k H 0

我是作为一个Springboot开发的新手来回答的。而且,我只是在尝试 AWS EB 和 SpringBoot 应用程序部署。

这是我的发现,

  1. WebMvcConfigurerAdapter 已弃用
  2. 仅当我们使用 SpringBootServletInitializer 扩展应用程序/主类时,SpringBoot 应用程序才能在 AWS EB 上无缝运行
  3. 我尝试了一个扩展 WebMvcConfigurerAdapter 的示例 HelloWorld 应用程序,该应用程序在本地主机上无缝运行,但在 AWS EB 上惨败。
  4. 我将应用程序类从 WebMvcConfigurerAdapter 扩展为 SpringBootServletInitializer,这在 localhost 和 AWS EB 上都有效。

我尝试的示例的灵感来自这里: https: //github.com/in28minutes/deploy-spring-boot-aws-eb/tree/master/03-spring-boot-web-application-h2

这是我从扩展 SpringBootServletInitializer 更改为 WebMvcConfigurerAdapter 的应用程序类,该类不起作用并给了我 404。 https://github.com/in28minutes/deploy-spring-boot-aws-eb/blob/master/03- spring-boot-web-application-h2/src/main/java/com/in28分钟/springboot/web/SpringBootFirstWebApplication.java

希望这有帮助......!

仍在找出当我们使用 WebMvcConfigurerAdapter 扩展 Application 类时收到 404 的原因。一旦找到原因,我将更新相同的答案。

谢谢...!