Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to request http method in AWS lambda handler in java?

How can I request the used HTTP Method in an AWS Lambda handler in JAVA? There is a argument 'context', but after a look on it, I can't request the used HTTP method.

HTTP-Methods are: GET, POST, PUT

BTW: Here is the answer for javascript: How to get the HTTP method in AWS Lambda?

best regards, lars

like image 475
Lars Wi Avatar asked Sep 14 '26 18:09

Lars Wi


1 Answers

You have several options on how you could receive the httpMethod in Java. One of the easiest would be to rename 'http-method' to 'httpMethod' in your Integration Request in API Gateway and then use the RequestHandler Interface for your Lambda handler which will marshal your JSON directly to a Java Object:

package example;

import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.Context; 

public class Hello implements RequestHandler<PojoRequest, PojoResponse> {

    public PojoResponse handleRequest(PojoRequest request, Context context) {
        System.out.println(String.format("HTTP method is %s.", request.getHttpMethod()));
        return new PojoResponse();
    }
}

Then you can create whatever Pojo you want to be the request, for example:

package example;

public class PojoRequest {
    private String firstName;
    private String lastName;
    private String httpMethod;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getHttpMethod() {
        return httpMethod;
    }

    public void setHttpMethod(String httpMethod) {
        this.httpMethod = httpMethod;
    }
}

See: http://docs.aws.amazon.com/lambda/latest/dg/java-handler-using-predefined-interfaces.html

like image 79
Dave Maple Avatar answered Sep 17 '26 06:09

Dave Maple



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!