com.amazonaws.apigateway.request.context missing in backend @RestController

未关闭
#1,551 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
42/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
aws, java, spring-boot
领域
api, backend, cloud

调研方向

从 issue 中展示的 LambdaHandler 入口点和 GeographicAddressController 开始,然后跟踪 API Gateway request context 如何暴露给 Spring controller 属性。使用列出的 Spring Boot 和容器版本重现该请求;当 @RequestAttribute 接收到包含 authorizer 详细信息的 AwsProxyRequestContext,而不是 null 时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

Issue: com.amazonaws.apigateway.request.context missing in backend @RestController

To help us debug your issue fill in the basic information below using the options provided

Serverless Java Container version: 2.1.4

Implementations: Spring Boot 3

Framework version: SpringBoot 3.5.3

Frontend service: REST API / API Gateway Proxy Request

Deployment method: SAM

Description

When using Spring Boot 3.5.3 with aws-serverless-java-container-springboot3:2.1.4,
the AwsProxyRequestContext is not being injected into a controller method when annotated with:

@RequestAttribute("com.amazonaws.apigateway.request.context")

This attribute is expected to contain the API Gateway request context, but instead it is always null.

Lambda Handler

import com.amazonaws.serverless.exceptions.ContainerInitializationException;
import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler;
import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class LambdaHandler implements RequestStreamHandler {
    private static final SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;

    static {
        try {
            handler = new SpringBootProxyHandlerBuilder<AwsProxyRequest>()
                    .defaultProxy()
                    .initializationWrapper(new InitializationWrapper())
                    .springBootApplication(Application.class)
                    .buildAndInitialize();
        } catch (ContainerInitializationException e) {
            throw new LambdaException("Could not initialize Lambda. ", e);
        }
    }

    @Override
    public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
        handler.proxyStream(inputStream, outputStream, context);
    }
}

Controller

import com.amazonaws.serverless.proxy.model.AwsProxyRequestContext;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class GeographicAddressController {

    private static final String API_GATEWAY_CONTEXT_PROPERTY = "com.amazonaws.apigateway.request.context";

    @GetMapping(path = "/geographicAddress", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Object> get(
            final @RequestHeader HttpHeaders reqHeaders,
            final @RequestParam Map<String, String> params,
            final @RequestAttribute(value = API_GATEWAY_CONTEXT_PROPERTY, required = false) AwsProxyRequestContext requestContext
    ) {
        // requestContext is always null, I would like to get AwsProxyRequestContext , and authorizer details
        return ResponseEntity.ok().build();
    }
}

Expected Behavior

The requestContext parameter should be populated with the AwsProxyRequestContext extracted from the incoming API Gateway event.

Actual Behavior

requestContext is always null inside the controller method.

Steps to Reproduce

  • Deploy a Spring Boot 3.5.3 application with aws-serverless-java-container-springboot3:2.1.4.
  • Use the Lambda handler shown above.
  • Add a controller method with @RequestAttribute("com.amazonaws.apigateway.request.context").
  • Invoke the API Gateway endpoint.
  • Observe that requestContext is always null.
主要语言
Java
星标
1.6k
派生
575
平均合并
1 天 15 小时
30 天内合并 PR
12

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

aws/serverless-java-container 的其他 Issue

查看 aws/serverless-java-container 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。