Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ApplicationContextException: Unable to start ServletWebServerApplicationContext #326

Closed
boroda4436 opened this issue Feb 26, 2020 · 15 comments · Fixed by #331
Closed

ApplicationContextException: Unable to start ServletWebServerApplicationContext #326

boroda4436 opened this issue Feb 26, 2020 · 15 comments · Fixed by #331

Comments

@boroda4436
Copy link

  • Framework version: aws-cli/2.0.0dev4 Python/3.7.4 Darwin/19.2.0 botocore/2.0.0dev3
  • Implementations: Spring Boot

Scenario

Run the spring-boot2 app locally

Expected behavior

the app starts without exceptions

Actual behavior

error happens:

image

Steps to reproduce

  1. Download the repo
  2. cd samples/springboot2/pet-store
  3. Run com.amazonaws.serverless.sample.springboot2.Application from your IDE as a simple spring-boot app.

Full log output

2020-02-26 15:57:01.303 INFO 59255 --- [ main] c.a.s.sample.springboot2.Application : Starting Application on Bogdans-MBP.mate.academy with PID 59255 (/Users/bchupika/IdeaProjects/Study/aws-serverless-java-container/samples/springboot2/pet-store/target/classes started by bchupika in /Users/bchupika/IdeaProjects/Study/aws-serverless-java-container)
2020-02-26 15:57:01.312 INFO 59255 --- [ main] c.a.s.sample.springboot2.Application : No active profile set, falling back to default profiles: default
2020-02-26 15:57:02.082 ERROR 59255 --- [ main] o.s.boot.SpringApplication : Application run failed

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:156)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:744)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:391)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:312)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204)
at com.amazonaws.serverless.sample.springboot2.Application.main(Application.java:47)
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:203)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:179)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:153)
... 8 common frames omitted

Process finished with exit code 1

@sapessi
Copy link
Collaborator

sapessi commented Feb 27, 2020

This is probably cause by the fact that we explicitly exclude the embedded Tomcat that implements ServletWebServerFactory.

To solve this, we could remove the exclusion from the dependency and simply shade Tomcat out when producing a build for AWS Lambda. Would that help?

@sapessi
Copy link
Collaborator

sapessi commented Mar 31, 2020

@boroda4436 I found the root cause and committed a fix in the v1.5 branch. We'll have this out with the next release shortly. I have also added a new feature to the builder that allows you to specify whether you want to start a WebApplicationType.SERVLET: Test sample

@sapessi sapessi added this to the Release 1.5 milestone Mar 31, 2020
@sapessi sapessi linked a pull request Apr 7, 2020 that will close this issue
@jchambers
Copy link

Hi! I'm afraid I'm still having this problem even after the 1.5 release. I created a new project with:

mvn archetype:generate -DgroupId=my.service -DartifactId=my-service -Dversion=1.0-SNAPSHOT \
       -DarchetypeGroupId=com.amazonaws.serverless.archetypes \
       -DarchetypeArtifactId=aws-serverless-springboot2-archetype \
       -DarchetypeVersion=1.5

Attempting to start the included Application either through an IDE or via Maven produces the same problem.

@sapessi
Copy link
Collaborator

sapessi commented Apr 16, 2020

The archetype by default excludes the embedded Tomcat since it is not useful in Lambda - this is why Spring can't find an implementation of ServletWebServerFactory. You can just uncomment the exclusion from the pom file:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
   <!--exclusions>
       <exclusion>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions-->
</dependency>

Fair point though. I'll create a separate issue to track moving this into a profile for the next release

@jchambers
Copy link

It's true that removing that exclusion eliminates the exception, but the Spring Boot application still doesn't start up all the way; instead, it appears to hang shortly after displaying the Spring Boot banner. I think there may be something else at play.

@jchambers
Copy link

I was mistaken, and I apologize for the false alarm; I missed that the default logging level for the project is WARN, and so all of the usual startup messages (at INFO or lower) weren't displayed. Everything is working as expected.

@sapessi
Copy link
Collaborator

sapessi commented Apr 17, 2020

No worries. For the next release, we'll make sure to include the correct profiles in the archetypes.

@bbehling
Copy link

bbehling commented May 1, 2020

I'm using version 1.5 and still need to comment out the exclusions. Was 1.5 supposed to include a fix where we didn't have to comment that out?

@SaptarshiMisra
Copy link

SaptarshiMisra commented Jan 4, 2021

I'm using version 1.5. Getting this error. Could you please share some thoughts

java.lang.NoClassDefFoundError: org/springframework/boot/WebApplicationType
	at com.amazonaws.serverless.proxy.spring.SpringBootProxyHandlerBuilder.<init>(SpringBootProxyHandlerBuilder.java:32)
	at com.amazonaws.serverless.proxy.spring.SpringBootLambdaContainerHandler.getAwsProxyHandler(SpringBootLambdaContainerHandler.java:88)
	at com.inboxy.api.StreamLambdaHandler.<clinit>(StreamLambdaHandler.java:20)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:348)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.WebApplicationType
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	... 5 more

this is how I am including the spring-boot-starter-web

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

@SamnnyWong
Copy link

SamnnyWong commented Nov 24, 2021

Hi, I am still having this issue, could anyone take a look?
Log:

2021-11-24 22:28:55.891  INFO 17394 --- [           main] c.a.s.sample.springboot2.Application     : Starting Application using Java 1.8.0_191 on Sams-MBP with PID 17394 (/Users/sam/Documents/spring/limit/pet-store/build/classes/java/main started by sam in /Users/sam/Documents/spring/limit/pet-store)
2021-11-24 22:28:55.892  INFO 17394 --- [           main] c.a.s.sample.springboot2.Application     : No active profile set, falling back to default profiles: default
2021-11-24 22:28:56.589 ERROR 17394 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:163)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:577)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:338)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332)
	at com.amazonaws.serverless.sample.springboot2.Application.main(Application.java:47)
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:210)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:180)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:160)
	... 8 common frames omitted

I change my dependency package like this
pom.xml:

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
<!--            <exclusions>-->
<!--                <exclusion>-->
<!--                    <groupId>org.springframework.boot</groupId>-->
<!--                    <artifactId>spring-boot-starter-tomcat</artifactId>-->
<!--                </exclusion>-->
<!--            </exclusions>-->
        </dependency>

after many tries, I still couldn't make it work.
We want to run and test it locally before deploying it to lambda.
Any helps will be appreciated.

@deki
Copy link
Collaborator

deki commented Nov 24, 2021

Hmm we have working sample projects in https://github.com/awslabs/aws-serverless-java-container/tree/master/samples that you can use to compare. Or you'll share your non-working sample and we'll have a look?

@SamnnyWong
Copy link

Hmm we have working sample projects in https://github.com/awslabs/aws-serverless-java-container/tree/master/samples that you can use to compare. Or you'll share your non-working sample and we'll have a look?
yes I git clone this sample app, deploy to lambda and test run it with no problem, but if I want to run it locally (open the sample app in idea and simply click run) it prompts this error. Did i miss anyhting?

@deki
Copy link
Collaborator

deki commented Nov 24, 2021

Well just remove the exclusion and then it will work. Just tried it and the sample was starting using Tomcat 9.0.50.

@SamnnyWong
Copy link

SamnnyWong commented Nov 26, 2021

I managed to recreate the issue. Since I am trying to integrate s3 into this project (as a starter code), so I click the "load Gradle project" and add the dependency into build.gradle. Then I try running the app it prompts this error. Also, I noticed the "edit configuration" window in idea prompts "Error: Module not specified".
Thanks in advance.

@deki
Copy link
Collaborator

deki commented Nov 26, 2021

If you are using Gradle, you have to edit the Gradle file, not the Maven POM. I suggest to try the command line tools first (e.g. by just running mvn spring-boot:run) to avoid IDE issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants