Spring Boot 2: StageName should be stripped from the path by default

未關閉
#401 7 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
45/100
Issue 類型
缺陷
描述清晰度
基本清楚
活躍度
停滯
技術堆疊
aws, java, spring-boot
領域
api, backend, cloud

研究方向

從 LambdaHandler 及其 SpringBootProxyHandlerBuilder 設定開始,接著將 issue 中顯示的 SAM HttpApi StageName 和 proxy 路徑與 Spring Boot 收到的 request 路徑進行比較。檢查 handler.stripBasePath 的使用方式,並讓 stage 名稱預設不再需要;當像 /dev/api/v1/some/resource 這樣的 request 能以 /api/v1/some/resource 進行路由且不會回傳 404 時,即表示完成。

由索引模型根據 Issue 內容生成。

描述

Serverless Java Container version: 1.5.2

Implementations: Spring Boot 2

Framework version: spring-boot-starter-parent 2.3.8.RELEASE

Frontend service: AWS::Serverless::HttpApi (API Gateway HTTP)

Deployment method: SAM CLI

Scenario

Configure a simple HTTP API proxy to Lambda using Spring Boot 2, and be able to route requests that ignore the stage-name.

Expected behavior

Stage name (which is part of the path by default) is stripped before spring routes/maps to a controller.

Actual behavior

Getting 404's b/c spring is mapping <stage>/<path> vs <path>; requires my code to be 'stage-name aware'.

Steps to reproduce

event-source in sam template for AWS::Serverless::Function

      Events:
        BaseAdmin:
          Type: HttpApi
          Properties:
            ApiId: !Ref Api
            Method: any
            Path: /{proxy+}
            PayloadFormatVersion: "1.0"
            RouteSettings:
              ThrottlingBurstLimit: 100
              ThrottlingRateLimit: 100
            TimeoutInMillis: 29000

api def in sam template:

  Api:
    Type: AWS::Serverless::HttpApi
    Properties:
      FailOnWarnings: true
      StageName: dev

lambda handler

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

    static {
        Application.envSetup();
        try {
            HANDLER = new SpringBootProxyHandlerBuilder<AwsProxyRequest>().defaultProxy()
                                                                          .asyncInit()
                                                                          .springBootApplication(Application.class)
                                                                          .buildAndInitialize();
        } catch (ContainerInitializationException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        // Want to not need the line below uncommented to work!
        // HANDLER.stripBasePath(System.getenv("STRIP_BASE_PATH"));
    }

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

Full log output

Paste the full log output from the Lambda function's CloudWatch logs
dev is the stage name; the mapping is for /api/v1

DEBUG DispatcherServlet - GET "/dev/api/v1/some/resource", parameters={}
DEBUG DispatcherServlet - Exiting from "ERROR" dispatch, status 404

This isn't the worst thing ever...but it's definitely counter-intuitive to me. Would also appreciate any feedback on my solution; I did try some of the config options mentioned on the wiki page, but none of them seemed to do the trick until i found handler.stripBasePath. If there is a way to do this w/o passing in the StageName, that'd be better. Having it work by default would obviously be best.

主要語言
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 摘要。