0

I am encountering an issue with my Spring Boot application deployed on AWS Lambda. I have created a REST API at api/users to retrieve a list of users.

The controller is defined as follows:

package com.example.lambdatest.controllers;
import java.util.List;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class ProfileController {

    @GetMapping("/users")
    public List<User> getUser() {
        return List.of(new User("John", "Doe", "john.doe@baeldung.com"), 
                       new User("John", "Doe", "john.doe-2@baeldung.com"));
    }
}

LambdaHandler.java

package com.example.lambdatest;

import com.amazonaws.serverless.exceptions.ContainerInitializationException;
import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
import com.amazonaws.serverless.proxy.spring.SpringBootLambdaContainerHandler;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class LambdaHandler implements RequestHandler<AwsProxyRequest, AwsProxyResponse> {
    private static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;

    static {
        try {
            handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(LambdatestApplication.class); }
        catch (ContainerInitializationException ex){
            throw new RuntimeException("Unable to load spring boot application",ex); }
    }

    @Override
    public AwsProxyResponse handleRequest(AwsProxyRequest input, Context context) {
        return handler.proxy(input, context);
    }
}

Where LambdatestApplication is the starting point of my application.

LambdatestApplication.java

package com.example.lambdatest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class LambdatestApplication {

    public static void main(String[] args) {
        SpringApplication.run(LambdatestApplication.class, args);
    }

}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>lambdatest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>lambdatest</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

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

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

        <dependency>
            <groupId>com.amazonaws.serverless</groupId>
            <artifactId>aws-serverless-java-container-springboot2</artifactId>
            <version>1.9.1</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.5.0</version>
                <configuration>
                        <createDependencyReducedPom>false</createDependencyReducedPom>
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>**/Log4j2Plugins.dat</exclude>
                                </excludes>
                            </filter>
                        </filters>
                </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>
        </plugins>
    </build>

</project>

I am expecting following response from the lambda:

[
    {
        "firstName": "John",
        "lastName": "Doe",
        "email": "john.doe@baeldung.com"
    },
    {
        "firstName": "John",
        "lastName": "Doe",
        "email": "john.doe-2@baeldung.com"
    }
]

I have tried changing jakarta dependency version (read somewhere that jakarta 6 is compatable with spring 3.1.2):

<dependency>
    <groupId>jakarta.servlet</groupId>
    <artifactId>jakarta.servlet-api</artifactId>
    <version>6.0.0</version>
    <scope>provided</scope>
</dependency>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

1 Answers1

0

aws-serverless-java-container-springboot2 only supports Spring Boot 2.x and you're using Spring Boot 3. You need to use aws-serverless-java-container-springboot3 as described in the Spring Boot 3 quickstart.

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242