简体   繁体   English

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

[英]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?如何在 JAVA 的 AWS Lambda 处理程序中请求使用的 HTTP 方法? There is a argument 'context' , but after a look on it, I can't request the used HTTP method.有一个参数'context' ,但在查看它之后,我无法请求使用的 HTTP 方法。

HTTP-Methods are: GET, POST, PUT HTTP 方法是:GET、POST、PUT

BTW: Here is the answer for javascript: How to get the HTTP method in AWS Lambda?顺便说一句:这是 javascript 的答案: How to get the HTTP method in AWS Lambda?

best regards, lars最好的问候,拉斯

You have several options on how you could receive the httpMethod in Java.关于如何在 Java 中接收httpMethod ,您有多种选择。 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:最简单'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();
    }
}

Then you can create whatever Pojo you want to be the request, for example:然后你可以创建任何你想要作为请求的 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;
    }
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM