How to handle mutiple request(post, get) within a single AWS Lambda request handler class in java

Viewed 167

I am new to AWS. I have a scenario wherein I am supposed to write Lambda functions for different requests(POST, GET, PUT, DELETE).

As I am doing it in java, I implemented the interface "RequestHandler". I need to override a method "handleRequest".

My question is: Is it possible to write a single java class with different functions to handle different requests(POST, GETC, etc) because there is only one "handleRequest"? I tried with a different randomly named method "handleReques"(removed t to test) to test but that didn't work and I got an exception on AWS console

{
  "errorMessage": "No public method named handleReques with appropriate method signature found on class

If possible can someone share how?

Or do I need to write different classes for different request types?

Thanks in advance

1 Answers

Absolutely you can. Your AWS Lambda function will most likely be triggered by an Amazon API Gateway. When you write your Lambda function you can specify the handler method to accept an API Gateway request and return an API Gateway response. All of these events are contained within the aws-lambda-java-events library.

Your handler will look something like this:

public class ApiRequestHandler implements RequestHandler<APIGatewayV2HTTPEvent, APIGatewayV2HTTPResponse> {

    @Override
    public APIGatewayV2HTTPResponse handleRequest(APIGatewayV2HTTPEvent event, Context context) {
 }
}

Within the APIGatewayV2HTTPEvent object is information about the request. Include the rest verb and the path. Using this you can create any routing you require.

Related