java - 调用 AWS API Gateway,GET 调用问题

标签 java amazon-web-services aws-sdk aws-api-gateway

<分区>

我正在从 Java 控制台应用程序调用以下 AWS Gateway API。在 postman 中,它与 AWS 签名完美配合。

获取 https://api.valorebooks.com/bid?isbns=[ “9780026840019”]

我正在使用 AWS SDK 和这个库。 https://github.com/rpgreen/apigateway-generic-java-sdk

控制台应用程序传递参数 isbns 但 API 抛出 HTTP:400

源 API:https://valorebooks.github.io/api/source/bid/

最佳答案

将参数转换为 json 字符串并将该字符串作为参数发布到查询字符串

Map<String, String> parameters = new LinkedHashMap<>();
List<String> productsIsbs = Arrays.asList(request.getProducts())
                    .stream().map(x -> x.getIsbn()).collect(Collectors.toList());
String params = new Gson().toJson(productsIsbs).replace("\"", "\'");
parameters.put("isbns", params);

GenericApiGatewayRequest apiGatewayRequest;
        try {

            apiGatewayRequest = new GenericApiGatewayRequestBuilder()
                            .withHeaders(headers)
                            .withHttpMethod(HttpMethodName.GET)
                            .withResourcePath("/bid")
                            .withParameters(parameters)
                            .build();

            GenericApiGatewayApacheResponse response = client.executeGetWithHttpClient(apiGatewayRequest);

            if(response.getHttpResponse().getStatusLine().getStatusCode() == 200) {
                String responseJson = response.getBody();
            }

        } catch (GenericApiGatewayException e) {   // exception thrown for any non-2xx response
            System.out.println(String.format("Client exception:%s - %s", e.getStatusCode(), e.getMessage()));
            e.printStackTrace();
        }

关于java - 调用 AWS API Gateway,GET 调用问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48548097/

相关文章:

amazon-web-services - 如何使用 Cognito Id(+配置)调用 AWS API 网关端点?

javascript - AWS S3 getObject JS SDK

amazon-web-services - 通过 CloudFormation 在启动模板中具有容量预留的 EKS 托管节点组不使用容量预留

amazon-web-services - AWS CodeBuild Docker 图像作为基础图像

node.js - AWS STS 担任角色 - InvalidClientTokenId : The security token included in the request is invalid

Java 类可访问性

java - 尝试将对象存储在该类型的变量中时,类型不兼容

java - 是否有独立于持久化技术的Java Data API

java - 与 T 不兼容的类型

amazon-web-services - CloudWatch 未接收来自外部区域的 Cloudtrail 日志