You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Java Lambda通过Function URL调用时无法读取请求体

问题解决:Lambda Function URL调用时请求体字段为null

核心原因

AWS Lambda控制台测试与Function URL的请求格式不同:

  • 控制台测试时,直接传入的是原始请求体JSON,Lambda会自动将其反序列化为UserRegistrationRequest对象;
  • 通过Function URL调用时,Lambda收到的是包装后的请求对象,原始请求体被包含在body字段中,直接用UserRegistrationRequest接收会因字段不匹配导致所有值为null。

解决方案

方法1:改用APIGatewayProxyRequestEvent接收请求并手动解析

这是兼容性最强的适配方式,通过Lambda事件对象获取原始请求体后再反序列化。

步骤1:添加依赖

确保项目引入以下依赖(以Maven为例):

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-lambda-java-events</artifactId>
    <version>3.11.1</version> <!-- 使用最新稳定版本 -->
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.15.2</version>
</dependency>

步骤2:修改Handler代码

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import com.fasterxml.jackson.databind.ObjectMapper;

public class UserRegistration implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
        APIGatewayProxyResponseEvent response = new APIGatewayProxyResponseEvent();
        try {
            // 解析请求体为UserRegistrationRequest对象
            UserRegistrationRequest request = objectMapper.readValue(input.getBody(), UserRegistrationRequest.class);
            context.getLogger().log("----------- Event body " + request);

            // 设置响应
            response.setStatusCode(200);
            response.setBody("{\"message\": \"Successfully Registered\"}");
            response.addHeader("X-Custom-Header", "application/json");
            response.addHeader("Content-Type", "application/json");
            return response;
        } catch (Exception e) {
            context.getLogger().log("Error: " + e.getMessage());
            response.setStatusCode(500);
            response.setBody("{\"message\": \"Registration failed\"}");
            response.addHeader("Content-Type", "application/json");
            return response;
        }
    }
}

UserRegistrationRequest类保持原定义不变(@Data注解已自动生成getter/setter,满足Jackson反序列化要求)。

方法2:配置Function URL的负载格式版本

如果不想修改Handler代码,可以尝试:

  • 进入AWS Lambda控制台的Function URL配置页面,将Payload format version设置为2.0;
  • 在Postman中确保请求头Content-Type: application/json,请求体选择raw -> JSON格式并传入正确的JSON内容。

注意:部分场景下仍可能存在格式差异,方法1的兼容性更强。

Postman请求验证

确保Postman的请求配置正确:

  • 请求方法:POST
  • 请求URL:你的Function URL地址
  • 请求头:添加Content-Type: application/json
  • 请求体:选择raw,格式选JSON,粘贴你的请求体内容:
{   
    "fullname": "ABC",
    "email": "abc@gmail.com",
    "mobile": "0611234123234",
    "password": "something",
    "tnc": "yes"
}

内容的提问来源于stack exchange,提问作者Sandeep Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 14:06:01