AWS Serverless Java Container 项目教程
1. 项目的目录结构及介绍
AWS Serverless Java Container 项目的目录结构如下:
serverless-java-container/
├── aws-serverless-java-container-core/
├── aws-serverless-java-container-jersey/
├── aws-serverless-java-container-spring/
├── aws-serverless-java-container-springboot3/
├── aws-serverless-java-container-struts/
├── aws-serverless-jersey-archetype/
├── aws-serverless-spring-archetype/
├── aws-serverless-springboot3-archetype/
├── aws-serverless-struts-archetype/
├── samples/
├── .gitignore
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── LICENSE
├── NOTICE
├── README.md
├── gha_build.sh
├── owasp-suppression.xml
├── pom.xml
├── spotbugs-excludeFilter.xml
目录介绍
aws-serverless-java-container-core/
: 核心库,提供基本的处理逻辑。aws-serverless-java-container-jersey/
: 支持 Jersey 框架的库。aws-serverless-java-container-spring/
: 支持 Spring 框架的库。aws-serverless-java-container-springboot3/
: 支持 Spring Boot 3 框架的库。aws-serverless-java-container-struts/
: 支持 Apache Struts 框架的库。aws-serverless-jersey-archetype/
: Jersey 框架的 archetype。aws-serverless-spring-archetype/
: Spring 框架的 archetype。aws-serverless-springboot3-archetype/
: Spring Boot 3 框架的 archetype。aws-serverless-struts-archetype/
: Apache Struts 框架的 archetype。samples/
: 示例代码。.gitignore
: Git 忽略文件。CODE_OF_CONDUCT.md
: 行为准则。CONTRIBUTING.md
: 贡献指南。LICENSE
: 许可证。NOTICE
: 通知文件。README.md
: 项目说明文档。gha_build.sh
: GitHub Actions 构建脚本。owasp-suppression.xml
: OWASP 抑制文件。pom.xml
: Maven 项目配置文件。spotbugs-excludeFilter.xml
: SpotBugs 排除过滤器。
2. 项目的启动文件介绍
项目的启动文件通常位于 samples/
目录下,具体取决于你使用的框架。以下是一个示例:
Spring Boot 示例
在 samples/springboot3/
目录下,主要的启动文件是 Application.java
:
package com.amazonaws.serverless.sample.springboot3;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Jersey 示例
在 samples/jersey/
目录下,主要的启动文件是 LambdaHandler.java
:
package com.amazonaws.serverless.sample.jersey;
import com.amazonaws.serverless.proxy.jersey.JerseyLambdaContainerHandler;
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 JerseyLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
public LambdaHandler() {
handler = JerseyLambdaContainerHandler.getAwsProxyHandler(ApiConfig.class);
}
@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context)
throws IOException {
handler.proxyStream(inputStream, outputStream, context);
}
}