可以通过以下代码示例解决“BadRequestException: 当授权类型为无或无效时,无法更新路由。”的问题:
import com.amazonaws.services.apigatewayv2.AmazonApiGatewayV2Client;
import com.amazonaws.services.apigatewayv2.model.*;
public class Main {
public static void main(String[] args) {
String apiId = "your-api-id";
String routeId = "your-route-id";
String authorizationType = "AWS_IAM"; // 要更新的授权类型
AmazonApiGatewayV2Client apiGatewayClient = new AmazonApiGatewayV2Client();
// 获取现有的路由信息
GetRouteRequest getRouteRequest = new GetRouteRequest()
.withApiId(apiId)
.withRouteId(routeId);
GetRouteResult getRouteResult = apiGatewayClient.getRoute(getRouteRequest);
Route route = getRouteResult.getRoute();
// 更新授权类型
UpdateRouteRequest updateRouteRequest = new UpdateRouteRequest()
.withApiId(apiId)
.withRouteId(routeId)
.withAuthorizationType(authorizationType)
.withAuthorizerId(route.getAuthorizerId()) // 保持现有的授权器
.withModelSelectionExpression(route.getModelSelectionExpression())
.withOperationName(route.getOperationName())
.withRequestModels(route.getRequestModels())
.withRouteKey(route.getRouteKey())
.withTarget(route.getTarget());
UpdateRouteResult updateRouteResult = apiGatewayClient.updateRoute(updateRouteRequest);
System.out.println("路由更新成功");
}
}
请注意替换代码中的 your-api-id
和 your-route-id
为您的实际 API ID 和路由 ID。还要确保您的 AWS 认证凭证配置正确。这段代码将使用 AWS SDK for Java v1 来更新指定路由的授权类型为 AWS_IAM
。