我们有一个场景,我们需要检索在 AWS 上运行的 EC2 实例的描述信息。为此,我们使用 AWS Java SDK。在我们 90% 的用例中,com.amazonaws.services.ec2.model.Instance
类正是我们所需要的。但是,还有一个小用例,获取描述实例的原始 XML 是有益的。即,转换为 Instance
对象之前的 XML 数据。有什么方法可以使用 AWS Java SDK 获取 Instance
对象和 XML 字符串?有没有办法手动从一个转换为另一个?或者,我们是否会被迫使用 HttpClient
或类似的东西进行单独调用以获取 XML 数据?
最佳答案
通过添加请求处理程序创建 EC2Client 并重写 beforeUnmarshalling() 方法,如下所示
AmazonEC2ClientBuilder.standard().withRegion("us-east-1")
.withRequestHandlers(
new RequestHandler2() {
@Override
public HttpResponse beforeUnmarshalling(Request<?> request, HttpResponse httpResponse) {
// httpResponse.getContent() is the raw xml response from AWS
// you either save it to a file or to a XML document
return new HTTPResponse(...);
// if you consumed httpResponse.getContent(), you need to provide new HTTPResponse
}
}
).build():
关于java - 使用 AWS Java SDK 获取 EC2 实例 XML 描述?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46081991/