Dropwizard Jersey 客户端示例

Dropwizard Jersey Client Sample

Dropwizard official documentation 球衣客户端不可测试,有人有 dropwizard 球衣客户端样本吗?

Dropwizard手册给出了how to add configuration for and then build a Jersey client in your app的代码示例。

Jersey 文档本身包含 how to use a Client to make a request.

的详细信息(和代码示例)

作为一个稍微不相关的建议,我喜欢为我编写的每个服务编写一个客户端(作为一个单独的 maven 模块),其他库(以及其他 Dropwizard 服务)随后可以使用该客户端与该服务进行通信。这让我可以将与服务交互的所有细节(例如,如何构建路径、类 将结果编组为什么)封装在一个地方,这样我就可以向外界展示一个漂亮、简单的 POJO 接口。请注意,这意味着在客户端和服务之间共享模型表示,几乎按照 the suggestion in the Dropwizard docs.

示例客户端可能如下所示:

public class MyClient {
    private static final String RESOURCE_PATH = "resource-path";
    private static final DateTimeFormatter FORMATTER = ISODateTimeFormat.dateTimeNoMillis();

    private final Client jerseyClient;
    private final String targetUrl;

    public MyClient(Client jerseyClient, String targetUrl) {
        this.jerseyClient = jerseyClient;
        this.targetUrl = targetUrl;
    }

    public List<CustomModelClass> getSomeResource(Interval someParam) {
        WebTarget webResource = jerseyClient.target(targetUrl).path(RESERVATIONS_PATH);

        webResource = webResource.queryParam("startTime", someParam.getStart().toString(FORMATTER));
        webResource = webResource.queryParam("endTime", someParam.getEnd().toString(FORMATTER));

        Invocation.Builder invocationBuilder = webResource.request(MediaType.APPLICATION_JSON_TYPE);
        Response response = invocationBuilder.get();

        return response.readEntity(new GenericType<List<CustomModelClass>>(){});
    }
}

我发现在 Dropwizard 中实现我的客户端也有点挑战。所以我想做出贡献,以防万一有人遇到这个问题。 这是 Dropwizard (v1.0.5) 中的客户端,它使用 Multipart 调用 POST Web 服务。也可以使用 GET 通过 Web 服务访问客户端。

我的依赖项 pom.xml:

<dependencies>
<dependency>
    <groupId>io.dropwizard</groupId>
    <artifactId>dropwizard-core</artifactId>
    <version>${dropwizard.version}</version>
</dependency>
<dependency>
    <groupId>io.dropwizard</groupId>
    <artifactId>dropwizard-assets</artifactId>
    <version>${dropwizard.version}</version>
</dependency>
<dependency>
    <groupId>io.dropwizard</groupId>
    <artifactId>dropwizard-forms</artifactId>
    <version>${dropwizard.version}</version>
</dependency>
<dependency>
    <groupId>io.dropwizard</groupId>
    <artifactId>dropwizard-client</artifactId>
    <version>${dropwizard.version}</version>
</dependency>

</dependencies>

这是我的 Dropwizard 应用程序 (Client2PostApplication.java):

    public class Client2PostApplication extends Application<Client2PostConfiguration> {
    public static void main(String[] args) throws Exception {
        new Client2PostApplication().run(args);
    }

    @Override
    public void initialize(Bootstrap<Client2PostConfiguration> bootstrap) {
        bootstrap.addBundle(new MultiPartBundle());
    }

    @Override
    public void run(Client2PostConfiguration configuration,
                Environment environment) throws Exception {

        environment.jersey().register(MultiPartFeature.class);
        JerseyClientConfiguration conf = configuration.getJerseyClientConfiguration();

        conf.setChunkedEncodingEnabled(false);

        final Client client = new JerseyClientBuilder(environment).using(conf).build(getName());
        environment.jersey().register(new Client2Post(client));
        environment.jersey().register(new MyPostResource());       
    }
}

这是我的配置 (Client2PostConfiguration.java):

public class Client2PostConfiguration extends Configuration {

    @Valid
    @NotNull
    private JerseyClientConfiguration jerseyClient = new JerseyClientConfiguration();

    @JsonProperty("jerseyClient")
    public JerseyClientConfiguration getJerseyClientConfiguration() {
        return jerseyClient;
    }
}

现在,post 网络服务 (MyPostResource.java):

@Path("/testpost")

public class MyPostResource {

    public MyPostResource() {

    }

    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Timed
    public String test(
        @FormDataParam("foo") String testData) throws IOException {
        return testData;
    }
}

最后,客户 (Client2Post.java):

@Produces(MediaType.TEXT_PLAIN)
@Path("/client")
public class Client2Post {

    private Client client;

    public Client2Post(Client client) {
        this.client = client;
    }

    @GET
    @Path("/test")
    public String testPost() {

    final Invocation.Builder request = client.target("http://localhost:8080/testpost").register(MultiPartFeature.class).request();

    final FormDataMultiPart entity = new FormDataMultiPart()
            .field("foo", "bar");

    final String response = request.post(Entity.entity(entity, entity.getMediaType()), String.class);

    return response;

    }
}

完整的源代码可以从here下载。