从 dropwizard 调用外部 API

Calling external API from dropwizard

我对 Dropwizard 和学习它(以及 API 一般的开发)还很陌生。

我正在研究 Dropwizard 构建一个 API,它应该在某个时候调用另一个 API。

更准确地说,我有一个 json/yaml 文件,我会将其转换为另一个 json 文件,供外部 API 使用。

我正在构建的API包括整​​个过程

现在我专注于最后一部分:调用外部 API(使用固定的 json)。那么如何从 Dropwizard 中调用外部 API?

这是 sudo 代码:

@Path("/my_api")
public class HelloResource {

@GET
@Produces(MediaType.APPLICATION_JSON)
public hit_external() {
    // call "my.external.api/ext_api"

}
}

非常感谢任何 comments/suggestions/link 参考。

你可以使用 Dropwizard Client module, which relies on the Apache HttpClient (default) or the Jersey Client. Once the client is configured in the DW application just use that to make the external calls. For an higher-level abstraction Feign 是一个不错的选择。

请注意,在对外部 Web 服务进行同步调用时,如果应用程序正在等待其他服务,则应用程序可能 运行 没有可用线程。如果可能,您应该使此调用异步,例如使用要处理的外部调用队列并让 API 的客户端检查该请求的状态。

感谢 Andre,我最终使用了 Dropwizard 客户端(Jersey 客户端):

import javax.ws.rs.client.ClientBuilder;


    Client client = ClientBuilder.newClient();
    String result = client.target("http://path_to_external_resource").request().get(String.class);

    return result;