简体   繁体   English

AWS API Gateway请求体作为Java POJO的功能

[英]AWS API Gateway request body as Java POJO for function

I was just having a really basic problem using aws-lambda, API Gateway and the serverless framework. 我只是在使用aws-lambda,API网关和无服务器框架时遇到了一个非常基本的问题。 I just wanted to hand over the body of a post request as a Java POJO. 我只是想将一个post请求的主体移交给Java POJO。

Okay, so here's the setup: 好的,所以这是设置:

POJO: POJO:

public class Person {
    private String lastName;
    private string firstName;

    ... Setters and Getters omitted
}

Handler: 处理器:

public class PersonHandler implements RequestHandler<Person, ApiGatewayResponse> {
    @Override
    public ApiGatewayResponse handleRequest(lastNamePerson person, Context context) {
        //... do something
     }
}

And the payload in the post's request body would be 并且帖子的请求正文中的有效负载将是

{
    "lastName" : "John",
    "firstName" : "Doe"
}

And, last but not least the serverless.yml 并且,最后但并非最不重要的是serverless.yml

{
...
functions:person
handler:com.serverless.handler
event:
  -http:
    path:person
    method:post
...
}

Well, looks pretty straight forward, doesn't it? 好吧,看起来很直接,不是吗?

Unfortunately, it's not that simple. 不幸的是,它并不那么简单。 The Person POJO will always be empty when calling the function. 调用函数时,Person POJO将始终为空。 How can we give the body as a POJO in AWS API Gateway & Lambda? 我们如何在AWS API Gateway&Lambda中将身体作为POJO?

Well, through prolonged research and some guessing I found the answer and decided to post it here for future me (and others) to find. 好吧,通过长时间的研究和一些猜测,我找到了答案,并决定将它发布在这里,以供将来我(和其他人)找到。

But first, let's take a look at the actual problem. 但首先,我们来看看实际问题。 The body will not be in the root but under input.body, and then Jackson doesn't know where to find your person. 身体不会在根部,而是在输入体内,然后杰克逊不知道在哪里找到你的人。

So, first we need to change from lambda-proxy-integration to lambda-integration. 所以,首先我们需要从lambda-proxy-integration改为lambda-integration。

And then we need to tell the integration to hand over the body as payload to the function. 然后我们需要告诉集成将身体作为有效负载移交给函数。

This gives us the following serverless.yml: 这给了我们以下serverless.yml:

{
...
functions:person
handler:com.serverless.handler
event:
  -http:
    path:person
    method:post
    integration:lambda
    request:
      template:
        application/json:'$input.body'
...
}

e voila, now your POJO will be populated. 瞧,现在你的POJO将被填充。 Hope this helps, and let me know if anybody found a simpler or better solution to this. 希望这会有所帮助,如果有人找到更简单或更好的解决方案,请告诉我。

Sources: 资料来源:

https://serverless.com/framework/docs/providers/aws/events/apigateway/#request-templates https://serverless.com/framework/docs/providers/aws/events/apigateway/#request-templates

Could not parse request body into json: Unexpected character (\\'-\\' (code 45)) AWS Lambda + API + Postman (for formatting the yml) 无法将请求主体解析为json:意外字符(\\' - \\'(代码45))AWS Lambda + API + Postman (用于格式化yml)

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

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