aws / serverless-java-container

A Java wrapper to run Spring, Spring Boot, Jersey, and other apps inside AWS Lambda.
https://aws.amazon.com/serverless/
Apache License 2.0
1.5k stars 560 forks source link

NPE on ServletContainer when using Spring Boot with Jersey #345

Open nytins opened 4 years ago

nytins commented 4 years ago

Scenario

Trying to run a Spring Boot with Jersey api

Expected behavior

Returns successful response

Actual behavior

Returns 502 Bad Gateway error

Steps to reproduce

  1. Deploy the lambda
  2. Invoke the API

Full log output

c.a.s.p.internal.LambdaContainerHandler : Error while handling request
java.lang.NullPointerException: null
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:365) ~[task/:na]
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:318) ~[task/:na]
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205) ~[task/:na]
at com.amazonaws.serverless.proxy.internal.servlet.FilterChainManager$ServletExecutionFilter.doFilter(FilterChainManager.java:356) ~[task/:na]
at com.amazonaws.serverless.proxy.internal.servlet.FilterChainHolder.doFilter(FilterChainHolder.java:90) ~[task/:na]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[task/:na]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[task/:na]
at com.amazonaws.serverless.proxy.internal.servlet.FilterChainHolder.doFilter(FilterChainHolder.java:90) ~[task/:na]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[task/:na]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[task/:na]
at com.amazonaws.serverless.proxy.internal.servlet.FilterChainHolder.doFilter(FilterChainHolder.java:90) ~[task/:na]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[task/:na]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[task/:na]
at com.amazonaws.serverless.proxy.internal.servlet.FilterChainHolder.doFilter(FilterChainHolder.java:90) ~[task/:na]
at com.amazonaws.serverless.proxy.internal.servlet.AwsLambdaServletContainerHandler.doFilter(AwsLambdaServletContainerHandler.java:156) ~[task/:na]
at com.amazonaws.serverless.proxy.spring.SpringBootLambdaContainerHandler.handleRequest(SpringBootLambdaContainerHandler.java:180) ~[task/:na]
at com.amazonaws.serverless.proxy.spring.SpringBootLambdaContainerHandler.handleRequest(SpringBootLambdaContainerHandler.java:53) ~[task/:na]
at com.amazonaws.serverless.proxy.internal.LambdaContainerHandler.proxy(LambdaContainerHandler.java:211) [task/:na]
at com.amazonaws.serverless.proxy.internal.LambdaContainerHandler.proxyStream(LambdaContainerHandler.java:254) [task/:na]
at com.capitalone.examples.aws.alb.lambda.StreamLambdaHandler.handleRequest(StreamLambdaHandler.java:30) [task/:na]
at lambdainternal.EventHandlerLoader$2.call(EventHandlerLoader.java:909) [LambdaSandboxJava-1.0.jar:na]
at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:341) [LambdaSandboxJava-1.0.jar:na]
at lambdainternal.AWSLambda.<clinit>(AWSLambda.java:63) [LambdaSandboxJava-1.0.jar:na]
at java.lang.Class.forName0(Native Method) [na:1.8.0_201]
at java.lang.Class.forName(Class.java:348) [na:1.8.0_201]
at lambdainternal.LambdaRTEntry.main(LambdaRTEntry.java:119) [LambdaJavaRTEntry-1.0.jar:na]
sapessi commented 4 years ago

Hi @nytins - I have not tried to run a JAX-RS application inside SpringBoot. Do you have some sample code I can test with?

Lucas3oo commented 3 years ago

Hi @sapessi

I really want to use Jersey for JAX-RS but within Spring boot too.

You can in any Spring boot setup depend on spring jersey starter jar. Check this link https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-jersey

I have a small sample which is a bit old here: https://github.com/Lucas3oo/jersey-spring

deki commented 2 years ago

Hi, I was able to reproduce the issue by following https://docs.spring.io/spring-boot/docs/current/reference/html/web.html#web.servlet.jersey and applying it to the Spring Boot 2 Petstore.

aws-serverless-java-container-jersey isn't used in this scenario and that's why Jersey fails with an NPE (WebComponent isn't initialized as org.glassfish.jersey.servlet.ServletContainer.init was never called).

I think to make it work we need to combine the functionalities of aws-serverless-java-container-springboot2 and aws-serverless-java-container-jersey.