如何在java中的AWS lambda处理程序中请求http方法?

时间:2017-01-11 15:17:26

标签: java amazon-web-services lambda aws-lambda

如何在JAVA中的AWS Lambda处理程序中请求使用的HTTP方法?有一个参数'context',但在查看之后,我无法请求使用的HTTP方法。

HTTP方法是:GET,POST,PUT

BTW:以下是javascript:How to get the HTTP method in AWS Lambda?

的答案

最好的问候, 拉斯

1 个答案:

答案 0 :(得分:2)

您可以通过多种方式了解如何在Java中接收httpMethod。最简单的方法之一是在API网关中的集成请求中将'http-method'重命名为'httpMethod',然后为您的Lambda处理程序使用RequestHandler接口,该接口将直接将JSON编组到Java对象:

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();
    }
}

然后你可以创建你想成为请求的任何Pojo,例如:

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;
    }
}

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