1

I have implemented a simple AWS lambda application with spring cloud function. When i create labmda function and send the request in AWS then the following error has thrown.

{
  "errorMessage": "Error loading class com.benz.aws.lambda.api.handler.OrderHandler: org/springframework/cloud/function/adapter/aws/SpringBootRequestHandler",
  "errorType": "java.lang.NoClassDefFoundError"
}

functon package

 @Component
    public class OrderFunction implements ApplicationContextInitializer<GenericApplicationContext>
    {
    
        private OrderDao orderDao;
    
        @Autowired
        public OrderFunction(OrderDao orderDao)
        {
            this.orderDao=orderDao;
        }

    @Bean
    public Function<String,Object> getOrderByName()
    {
        return name-> {
            System.out.println("Hello Benz");
            return name;
        };
    }

    @Override
    public void initialize(GenericApplicationContext context) {
        context.registerBean("getOrderByName", FunctionRegistration.class,
                () -> new FunctionRegistration<Function<String,Object>>(getOrderByName())
                        .type(FunctionType.from(String.class).to(Object.class).getType()));
    }
}

handler package

public class OrderHandler extends SpringBootRequestHandler<String,Object> {

}

pom.xml

<dependencies>

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-function-adapter-aws</artifactId>
            <version>3.0.6.RELEASE</version>
        </dependency>


<dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-events</artifactId>
            <version>3.8.0</version>
        </dependency>

     <dependency>
                <groupId>com.amazonaws</groupId>
                <artifactId>aws-lambda-java-core</artifactId>
                <version>1.2.1</version>
            </dependency>
    
    </dependencies>
    
    <build>
            <plugins>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
                </plugin>
    
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
    
                    <dependencies>
                        <dependency>
                            <groupId>org.springframework.boot.experimental</groupId>
                            <artifactId>spring-boot-thin-layout</artifactId>
                            <version>${wrapper.version}</version>
                        </dependency>
                    </dependencies>
    
                    <configuration>
                        <excludes>
                            <exclude>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                            </exclude>
                        </excludes>
                        <finalName>
                            boot-lambda
                        </finalName>
                    </configuration>
                </plugin>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>3.2.4</version>
                    <configuration>
                        <createDependencyReducedPom>
                            false
                        </createDependencyReducedPom>
                        <shadedArtifactAttached>
                            true
                        </shadedArtifactAttached>
                        <shadedClassifierName>
                            aws
                        </shadedClassifierName>
                    </configuration>
                </plugin>
    
            </plugins>
        </build>

in AWS

enter image description here enter image description here enter image description here

I have tried to debug it many ways but didn't work. If there any simple ways to do the above things without extending SpringBootRequestHandler class

Nafaz M N M
  • 1,558
  • 2
  • 27
  • 41

2 Answers2

0

Looks like you are having issues with using the AWS SDK for Java within a Spring app. Using the AWS SDK for Java and the Spring Framework works fine when you setup the POM correctly. When there are issues, its typically because the required dependencies are not set correctly in the POM file.

There are various examples of using the Spring framework with the AWS SDK for Java V2 (no examples with V1 as Amazon recommends moving to V2). For example.

Creating your first AWS Java web application

If you follow that example, you will successfully create a sample app that uses the AWS SDK for Java and the Spring Framework.

smac2020
  • 9,637
  • 4
  • 24
  • 38
0

Indeed it looks like a problem with packaging as it appears that it can not find your class in the JAR.

Also, keep in mind that SpringBootRequestHandler has been deprecated and will be removed in due time. A simpler (new) approach is described in details here

You can also take one of the samples available here. There are actually 3 of them there, but the function-sample-aws is the simplest one and directly relates to what you're trying to do.

Oleg Zhurakousky
  • 5,820
  • 16
  • 17