使用适用于 Java 2.x 的 SDK 的 API 网关示例 - AWS SDK for Java 2.x

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用适用于 Java 2.x 的 SDK 的 API 网关示例

以下代码示例向您展示了如何使用 with API Gateway 执行操作和实现常见场景。AWS SDK for Java 2.x

操作是大型程序的代码摘录,必须在上下文中运行。虽然操作向您展示了如何调用单个服务函数,但您可以在其相关场景和跨服务示例中查看操作的上下文。

场景是展示如何通过在同一服务中调用多个函数来完成特定任务的代码示例。

每个示例都包含一个链接GitHub,您可以在其中找到有关如何在上下文中设置和运行代码的说明。

主题

操作

以下代码示例显示了如何创建 API 网关 REST API。

SDK for Java 2.x
注意

还有更多GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

public static String createAPI( ApiGatewayClient apiGateway, String restApiId, String restApiName) { try { CreateRestApiRequest request = CreateRestApiRequest.builder() .cloneFrom(restApiId) .description("Created using the Gateway Java API") .name(restApiName) .build(); CreateRestApiResponse response = apiGateway.createRestApi(request); System.out.println("The id of the new api is "+response.id()); return response.id(); } catch (ApiGatewayException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; }

以下代码示例显示了如何删除 API 网关 REST API。

SDK for Java 2.x
注意

还有更多GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

public static void deleteAPI( ApiGatewayClient apiGateway, String restApiId) { try { DeleteRestApiRequest request = DeleteRestApiRequest.builder() .restApiId(restApiId) .build(); apiGateway.deleteRestApi(request); System.out.println("The API was successfully deleted"); } catch (ApiGatewayException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }

以下代码示例显示了如何删除部署。

SDK for Java 2.x
注意

还有更多GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

public static void deleteSpecificDeployment(ApiGatewayClient apiGateway, String restApiId, String deploymentId) { try { DeleteDeploymentRequest request = DeleteDeploymentRequest.builder() .restApiId(restApiId) .deploymentId(deploymentId) .build(); apiGateway.deleteDeployment(request); System.out.println("Deployment was deleted" ); } catch (ApiGatewayException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }

以下代码示例显示了如何部署 API 网关 REST API。

SDK for Java 2.x
注意

还有更多GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

public static String createNewDeployment(ApiGatewayClient apiGateway, String restApiId, String stageName) { try { CreateDeploymentRequest request = CreateDeploymentRequest.builder() .restApiId(restApiId) .description("Created using the AWS API Gateway Java API") .stageName(stageName) .build(); CreateDeploymentResponse response = apiGateway.createDeployment(request); System.out.println("The id of the deployment is "+response.id()); return response.id(); } catch (ApiGatewayException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return "" ; }