0

I have a spring boot application with 2 GET end point. I am trying to include Lambda handler request and deploy as AWS Lambda function.

As a first step, I added below code for RequestStreamHandler.

public class AWSLambdaHandler implements RequestStreamHandler {
    private static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
    static {
        try {
            handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(AWSTestApplication.class);
        } catch (ContainerInitializationException e) {
            // Re-throw the exception to force another cold start
            e.printStackTrace();
            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 by the mapper
        outputStream.close();
    }
}

Application class:

package com.example.awstest.client;

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

@SpringBootApplication
public class AWSTestApplication  extends SpringBootServletInitializer {

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

Q: I have 2 GET end points in this spring boot service. Is this way of implementing Lambda handler request is enough or Do I need to add more code for the lambda function to handle this end point requests differently?

Controller class (just for the question 2):

@RestController

public class AWSTestController {

        @Autowired
        private AWSTestServiceDAO awstestServiceDAO;

        @CrossOrigin(origins = "*")
        @GetMapping("/searchAllData")
        public List<TESTData> searchAllData() {

            List<TESTData> dataList = awstestServiceDAO.getAllData();
            return dataList;
         }

        @CrossOrigin(origins = "*")
        @GetMapping("/searchDataByUser/{userno}")
        public List<TESTData> searchDataByUser(@PathVariable Integer userno) {

            List<TESTData> dataList = awstestServiceDAO.findDataByMemberNo(userno);
            return dataList;
        }

}
Stella
  • 1,728
  • 5
  • 41
  • 95

1 Answers1

0

The error you are experiencing is related to the method getAwsProxyHandler() in the SpringBootLambdaContainerHandler class. The error message indicates that the method is expecting a parameter of type Class<? extends WebApplicationInitializer>, but you are passing AWSTestApplication.class, which is of type Class.

To fix this error, you can try the following:

Ensure that AWSTestApplication implements the WebApplicationInitializer interface. If it doesn't already, you can modify your application class as follows:

@SpringBootApplication
public class AWSTestApplication extends SpringBootServletInitializer 
implements WebApplicationInitializer {
    // ... your code ...
}

Update the getAwsProxyHandler() call in your AWSLambdaHandler class to use a wildcard (?) for the generic type:

handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(AWSTestApplication.class);

By using Class<?> instead of Class, it should resolve the type mismatch error.

Regarding your second question about handling different endpoint requests in the Lambda function, you can achieve that by implementing separate methods in your AWSTestController class for each endpoint. The code you provided already includes two GET endpoints: /searchAllData and /searchDataByUser/{userno}. These endpoints can be invoked by making GET requests to the corresponding URLs.

Make sure you configure the necessary dependencies and mappings in your Spring Boot application to handle these requests correctly.

  • Thank you very much for the response. The error was solved already, so i had remove that from my actual query, but again thanks for the explanation. Regarding question 2, yes, my spring boot application is already working with 2 end points. My doubt was, after adding Lambda handler, do i need to change anything more since lambda handler has been added now. But, i understand, i don't need to do anything much, end points will work as usual but from lambda function. – Stella Jun 28 '23 at 05:30
  • Yes, you are correct. After adding the Lambda handler and configuring your Spring Boot application to work with AWS Lambda, you don't need to make any significant changes to your existing endpoint implementations. –  Jun 28 '23 at 05:58
  • Can you pls help me on this question too? I don't know how to test Lambda function which holds my spring boot end points. https://stackoverflow.com/questions/76577207/spring-boot-deployed-in-lambda-and-function-url-do-nothing – Stella Jun 28 '23 at 23:11
  • Hi @user19682350 Can you please help me on this request too? https://stackoverflow.com/questions/76577207/spring-boot-deployed-in-lambda-and-function-url-do-nothing – Stella Jun 29 '23 at 01:58
  • All three of your recent answers here appear likely to have been entirely or partially written by AI (e.g., ChatGPT). Please be aware that [posting AI-generated content is not allowed here](//meta.stackoverflow.com/q/421831). If you used an AI tool to assist with any answer, I would encourage you to delete it. – NotTheDr01ds Jul 05 '23 at 11:27
  • **Readers should review this answer carefully and critically, as AI-generated information often contains fundamental errors and misinformation.** If you observe quality issues and/or have reason to believe that this answer was generated by AI, please leave feedback accordingly. The moderation team can use your help to identify quality issues. – NotTheDr01ds Jul 05 '23 at 11:27