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

Getting issue while running Spring Boot REST service on aws lambda #206

Closed
Manish-SM opened this issue Oct 26, 2018 · 10 comments
Closed

Getting issue while running Spring Boot REST service on aws lambda #206

Manish-SM opened this issue Oct 26, 2018 · 10 comments

Comments

@Manish-SM
Copy link

Manish-SM commented Oct 26, 2018

  • Framework version: 2.0
  • Implementations: Spring Boot

My Handler:

public class StreamLambdaHandler implements RequestStreamHandler {
    private static Logger logger = LoggerFactory.getLogger(StreamLambdaHandler.class);     

    public static final SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
 
    static { 
       try { 
           handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(StudentServicesApplication.class);
           handler.activateSpringProfiles("lambda");
      } catch (ContainerInitializationException e) { 
    	   logger.error("Cannot initialize Spring container", e);
           throw new RuntimeException("Could not initialize Spring Boot application", e); 
       } 
    }

    @Override 
    public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
        handler.proxyStream(inputStream, outputStream, context);
        // just in case it wasn't closed 
        outputStream.close(); 
    }
}

@ComponentScan("com")
@SpringBootApplication
@EnableWebMvc
@Profile("lambda")
public class StudentServicesApplication extends SpringBootServletInitializer {	public static void main(String[] args) {
		SpringApplication.run(StudentServicesApplication.class, args);
	}
}

POM.XML

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.new</groupId>
	<artifactId>REST-Lambda</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>REST-Lambda</name>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.6.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		<dependency>
			<groupId>com.amazonaws.serverless</groupId>
			<artifactId>aws-serverless-java-container-spring</artifactId>
			<version>1.2</version>
		</dependency>	
		
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-core</artifactId>
            <version>1.1.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        
		<dependency>
			<groupId>com.googlecode.json-simple</groupId>
			<artifactId>json-simple</artifactId>
		</dependency>
       
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-shade-plugin</artifactId>
				<configuration>
					<createDependencyReducedPom>false</createDependencyReducedPom>
				</configuration>
				<executions>
					<execution>
						<phase>package</phase>
						<goals>
							<goal>shade</goal>
						</goals>
						<configuration>
							<artifactSet>
								<excludes>
									<exclude>org.apache.tomcat.embed:*</exclude>
								</excludes>
							</artifactSet>
						</configuration>
					</execution>
				</executions>
			</plugin>

 			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<fork>true</fork>
					<executable>C:\Program Files\Java\jdk1.8.0_181\bin\javac</executable>
				</configuration>
			</plugin>
		</plugins>
		<resources>
        	<resource>
            	<directory>src/main/resources</directory>
            	<filtering>true</filtering>
        	</resource>
    	</resources>
	</build>
</project>

Getting this issue :

Error loading class com.lambda.StreamLambdaHandler: java.lang.ExceptionInInitializerError
java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dispatcherServlet' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:685)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1209)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1093)
at com.amazonaws.serverless.proxy.spring.SpringBootLambdaContainerHandler.initialize(SpringBootLambdaContainerHandler.java:183)
at com.amazonaws.serverless.proxy.spring.SpringBootLambdaContainerHandler.getAwsProxyHandler(SpringBootLambdaContainerHandler.java:77)
at com.lambda.StreamLambdaHandler.<clinit>(StreamLambdaHandler.java:27)
@sapessi
Copy link
Collaborator

sapessi commented Oct 26, 2018

Hi @Manish-SM, does the application work if you use SpringBoot 1.5.17.RELEASE? We have not yet officially added support for SpringBoot 2.x - it is tracked in issue #181.

@Manish-SM
Copy link
Author

Hi @sapessi, Thanks for reply. I tried with SpringBoot 1.5.17 version and getting some different error now.
Error:
Error loading class com.lambda.StreamLambdaHandler: java.lang.ExceptionInInitializerError
java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dispatcherServlet' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1209)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1086)
at com.amazonaws.serverless.proxy.spring.SpringBootLambdaContainerHandler.initialize(SpringBootLambdaContainerHandler.java:183)
at com.amazonaws.serverless.proxy.spring.SpringBootLambdaContainerHandler.getAwsProxyHandler(SpringBootLambdaContainerHandler.java:77)
at com.lambda.StreamLambdaHandler.(StreamLambdaHandler.java:27)
... 2 more

@sapessi
Copy link
Collaborator

sapessi commented Oct 26, 2018

Can you remove the spring-boot-starter as a dependency, you should only need the spring-boot-starter-web. Any chance the project code is on GitHub and we can try to replicate?

@Manish-SM
Copy link
Author

REST-Lambda.zip
@sapessi, I tried with suggested solution but getting same error. I have uploaded zip file on this path. https://github.com/awslabs/aws-serverless-java-container/files/2520162/REST-Lambda.zip.
Can you please check and suggest? Thanks in advance.

@sapessi
Copy link
Collaborator

sapessi commented Oct 26, 2018

Remove the @Profile annotation from the StudentServicesApplication class. To load a profile, use the parameters to the getAwsProxyHandler method.

@Manish-SM
Copy link
Author

@sapessi, Thanks for your help. It works for me. I made mentioned changes after that deployed and tested with api gateway with JSON body and headers and it worked only with api gateway not with lambda.

@sapessi
Copy link
Collaborator

sapessi commented Oct 29, 2018

To test this directly in Lambda, you need to make sure you send an event shaped exactly like API Gateway's proxy event.

@sendpramod
Copy link

sendpramod commented Feb 13, 2019

Hi,
what do you mean by "event shaped exactly like API Gateway's proxy event".
I am getting exception while running a springboot2 application on aws but it works fine with sam local.
I have created a apigateway and invoking the lambda function through it

@Cpinto0594
Copy link

Hi,
what do you mean by "event shaped exactly like API Gateway's proxy event".

I got the same doubt, does anyone have any example of how to do it?

@DHAPARK

This comment was marked as spam.

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

No branches or pull requests

5 participants