[英]How to request http method in AWS lambda handler in java?
如何在 JAVA 的 AWS Lambda 处理程序中请求使用的 HTTP 方法? 有一个参数'context'
,但在查看它之后,我无法请求使用的 HTTP 方法。
HTTP 方法是:GET、POST、PUT
顺便说一句:这是 javascript 的答案: How to get the HTTP method in AWS Lambda?
最好的问候,拉斯
关于如何在 Java 中接收httpMethod
,您有多种选择。 最简单'http-method'
之一是在 API Gateway 的集成请求中将'http-method'
重命名为'httpMethod'
,然后将 RequestHandler 接口用于您的 Lambda 处理程序,它将直接将您的 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.