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

新手求助:在Java SpringBoot中使用AWS Lambda获取REST API数据

Java AWS Lambda调用REST API并记录日志实现方案

1. 依赖配置(Maven)

如果使用Java 11+,可以直接用原生java.net.http客户端,无需额外引入HTTP依赖;如果用Java 8及以下版本,推荐使用Apache HttpClient。以下是Maven pom.xml的核心依赖示例:

<!-- AWS Lambda核心依赖 -->
<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-lambda-java-core</artifactId>
    <version>1.2.21</version>
</dependency>
<!-- 可选:Apache HttpClient(Java 8及以下推荐) -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.14</version>
</dependency>
<!-- 日志依赖:Lambda默认支持SLF4J,无需额外配置即可输出到CloudWatch -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.36</version>
</dependency>

2. Lambda处理器实现代码

基于你提供的Java基础示例,修改实现REST API调用和日志记录:

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class LambdaHandler implements RequestHandler<Object, String> {
    // 初始化SLF4J日志实例,自动关联CloudWatch日志
    private static final Logger logger = LoggerFactory.getLogger(LambdaHandler.class);

    @Override
    public String handleRequest(Object input, Context context) {
        // 记录输入参数日志
        logger.info("Lambda收到输入参数: {}", input);
        
        // 调用Dummy REST API示例(这里用公开测试端点演示)
        String apiUrl = "https://jsonplaceholder.typicode.com/todos/1";
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(apiUrl))
                .GET()
                .build();

        try {
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
            // 记录API响应状态和内容
            logger.info("API调用响应状态码: {}", response.statusCode());
            logger.info("API响应内容: {}", response.body());
            
            return "API调用成功,响应状态码: " + response.statusCode();
        } catch (Exception e) {
            // 记录异常日志
            logger.error("调用REST API失败", e);
            return "API调用失败: " + e.getMessage();
        }
    }
}

3. 关键注意事项

  • 日志查看:使用SLF4J输出的日志会自动同步到AWS CloudWatch,你可以在Lambda控制台的「监控」标签页中查看对应日志组。
  • 网络权限:确保Lambda函数拥有出站互联网访问权限——如果配置了VPC,需要绑定带NAT网关的子网;未配置VPC的函数默认拥有互联网访问权限。
  • 打包部署:使用Maven Shade插件将所有依赖打包成Fat Jar,上传到Lambda即可。插件配置示例:
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.4.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
  • 超时设置:在Lambda控制台的「配置」-「常规配置」中,根据API响应时长调整超时时间(默认3秒,建议设置为10-30秒)。

内容的提问来源于stack exchange,提问作者Jay Jethava

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 08:43:32