简体   繁体   English

将 Spring Boot(使用 JSP)部署到 Elastic Beanstalk

[英]Deploy Spring Boot (with JSP) to Elastic Beanstalk

I am attempting to deploy my Spring Boot project to Amazon Elastic Beanstalk.我正在尝试将我的 Spring Boot 项目部署到 Amazon Elastic Beanstalk。 I have tested and have no issue if I use the default Thymeleaf configuration, but when I switch to JSP based setup I get 404's as it cannot find the JSP's (located in src/main/webapp/WEB-INF/jsp)如果我使用默认的 Thymeleaf 配置,我已经测试并且没有问题,但是当我切换到基于 JSP 的设置时,我得到 404,因为它找不到 JSP(位于 src/main/webapp/WEB-INF/jsp)

I have attempted to deploy the sample (spring-boot-sample-tomcat-jsp) and find that this as well gives me a 404 when I run the provided test.我试图部署示例(spring-boot-sample-tomcat-jsp)并发现当我运行提供的测试时这也给了我一个 404。

Here is how I have typically been configuring my Spring Boot Projects to allow for the use of JSP's.以下是我通常如何配置我的 Spring Boot 项目以允许使用 JSP。

  1. Add Jasper and JSTL to pom.xml将 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>
  2. Override default view resolver configuration覆盖默认视图解析器配置

    @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(); } }
  3. Create folder for JSP's (src/main/webapp/WEB-INF/jsp)为 JSP 创建文件夹 (src/main/webapp/WEB-INF/jsp)

Now this method works without issue until I deploy to Elastic Beanstalk where I find that no matter if I create a jar and use Java (in Elastic Beanstalk) or create a war and use Tomcat (in eb) I get a 404 when any controller tries to return a view in the WEB-INF/jsp folder.现在这种方法可以正常工作,直到我部署到 Elastic Beanstalk 在那里我发现无论我创建一个 jar 并使用 Java(在 Elastic Beanstalk 中)还是创建一个战争并使用 Tomcat(在 eb 中)当任何控制器尝试时我都会得到 404返回 WEB-INF/jsp 文件夹中的视图。

Is the above method for switching to JSP's not recommended?不推荐使用上述切换到 JSP 的方法吗? Is there a better way to configure Spring Boot to use Tomcat/Jasper/JSP's?有没有更好的方法来配置 Spring Boot 以使用 Tomcat/Jasper/JSP?

I have attempted the method provided in the Spring Boot Samples on github here我在这里尝试了 github 上的 Spring Boot Samples 中提供的方法

But what's interesting is if I run the provided test i get the same 404.但有趣的是,如果我运行提供的测试,我会得到相同的 404。

Any help would be greatly appreciated.任何帮助将不胜感激。 If there is a better way to deploy a Spring Boot project that utilizes JSP's I'd be happy to switch over, but currently I seem to have configured myself into a corner.如果有更好的方法来部署利用 JSP 的 Spring Boot 项目,我很乐意切换,但目前我似乎已经将自己配置到了一个角落。

Thx!谢谢!

I am answering as a novice developer in Springboot development.我作为 Springboot 开发的新手开发人员来回答。 And, I am just playing around with AWS EB and SpringBoot app deployment.而且,我只是在玩 AWS EB 和 SpringBoot 应用程序部署。

Here are my findings,这是我的发现,

  1. WebMvcConfigurerAdapter is deprecated WebMvcConfigurerAdapter 已弃用
  2. SpringBoot app works seamlessly on AWS EB only when we extend application/main class with SpringBootServletInitializer只有当我们使用 SpringBootServletInitializer 扩展应用程序/主类时,SpringBoot 应用程序才能在 AWS EB 上无缝运行
  3. I tried a sample HelloWorld application extending WebMvcConfigurerAdapter, which worked seamlessly on localhost and failed miserable on AWS EB.我尝试了一个扩展 WebMvcConfigurerAdapter 的示例 HelloWorld 应用程序,它在 localhost 上无缝运行,但在 AWS EB 上却惨遭失败。
  4. I switched the application class extending from WebMvcConfigurerAdapter to SpringBootServletInitializer, this worked on both localhost as well as AWS EB.我将扩展的应用程序类从 WebMvcConfigurerAdapter 切换到 SpringBootServletInitializer,这在 localhost 和 AWS EB 上都有效。

The example I tried is inspired from here: https://github.com/in28minutes/deploy-spring-boot-aws-eb/tree/master/03-spring-boot-web-application-h2我尝试的示例来自这里: https ://github.com/in28minutes/deploy-spring-boot-aws-eb/tree/master/03-spring-boot-web-application-h2

Here is the application class that I changed from extending SpringBootServletInitializer to WebMvcConfigurerAdapter, which didn't work and gave me 404. https://github.com/in28minutes/deploy-spring-boot-aws-eb/blob/master/03-spring-boot-web-application-h2/src/main/java/com/in28minutes/springboot/web/SpringBootFirstWebApplication.java这是我从将 SpringBootServletInitializer 扩展为 WebMvcConfigurerAdapter 更改的应用程序类,它不起作用并给了我 404。 https://github.com/in28minutes/deploy-spring-boot-aws-eb/blob/master/03- spring-boot-web-application-h2/src/main/java/com/in28minutes/springboot/web/SpringBootFirstWebApplication.java

Hopefully this helps ...!希望这会有所帮助......!

Still finding out a reason about why we receive 404 when we extend Application class with WebMvcConfigurerAdapter.当我们使用 WebMvcConfigurerAdapter 扩展 Application 类时,仍然找出我们收到 404 的原因。 I Will update this same answer, once I find a reason.一旦找到原因,我将更新相同的答案。

Thank you...!谢谢...!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Elastic BeansTalk CLI部署Spring Boot应用程序 - Deploy Spring Boot application with Elastic BeansTalk CLI 如何将Spring启动程序部署到Amazon Elastic Beanstalk? - How to deploy a Spring boot program to Amazon Elastic Beanstalk? 无法将Spring Boot战争部署到Elastic Beanstalk - Can't deploy Spring Boot war to Elastic Beanstalk Spring Boot无法在Elastic Beanstalk上运行 - Spring boot not working on Elastic Beanstalk 在使用 RDS 的 AWS (Elastic Beanstalk) 上部署 Spring 引导应用程序 - 应用程序未启动 - Deploy Spring Boot application on AWS (Elastic Beanstalk) that uses RDS - Application does not start 当我在 aws 弹性 beantalk 中部署 Spring Boot 项目时,如何压缩 Spring Boot 中的 MultipartFie[] 大小? - How can i compress the MultipartFie[] size in spring boot when i deploy the spring boot project in aws elastic beanstalk.? Elastic Beanstalk工作者层上的Spring Boot - Spring Boot on Elastic Beanstalk worker tier 弹性beantalk + Spring Boot + Array属性 - Elastic beanstalk + Spring Boot + Array properties 502 错误网关 Elastic Beanstalk Spring Boot - 502 bad gateway Elastic Beanstalk Spring Boot 带有SSL / HTTPS的Spring Boot项目无法在AWS Elastic Beanstalk上运行 - Spring Boot project with SSL / HTTPS not working on AWS Elastic Beanstalk
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM