Bipin Butala
Bipin Butala

Reputation: 175

Deploy Spring Boot (with JSP) to Elastic Beanstalk

I am attempting to deploy my Spring Boot project to 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)

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.

Here is how I have typically been configuring my Spring Boot Projects to allow for the use of JSP's.

  1. Add Jasper and JSTL to 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)

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.

Is the above method for switching to JSP's not recommended? Is there a better way to configure Spring Boot to use Tomcat/Jasper/JSP's?

I have attempted the method provided in the Spring Boot Samples on github here

But what's interesting is if I run the provided test i get the same 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.

Thx!

Upvotes: 5

Views: 1177

Answers (2)

Jitender Singh
Jitender Singh

Reputation: 1

I was facing similar problem. I was able to get response for simple string but could not access JSP pages. I resolved this by creating an environment > selecting tomcat as platform instead of java and deploying my war file there.

Upvotes: 0

Karthik H
Karthik H

Reputation: 1439

I am answering as a novice developer in Springboot development. And, I am just playing around with AWS EB and SpringBoot app deployment.

Here are my findings,

  1. WebMvcConfigurerAdapter is deprecated
  2. SpringBoot app works seamlessly on AWS EB only when we extend application/main class with SpringBootServletInitializer
  3. I tried a sample HelloWorld application extending WebMvcConfigurerAdapter, which worked seamlessly on localhost and failed miserable on AWS EB.
  4. I switched the application class extending from WebMvcConfigurerAdapter to SpringBootServletInitializer, this worked on both localhost as well as 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

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

Hopefully this helps ...!

Still finding out a reason about why we receive 404 when we extend Application class with WebMvcConfigurerAdapter. I Will update this same answer, once I find a reason.

Thank you...!

Upvotes: 0

Related Questions