【问题标题】:Dropwizard Jersey Client SampleDropwizard Jersey 客户端样本
【发布时间】:2015-08-28 08:03:59
【问题描述】:

Dropwizard official documentation jersey Client 不可测试,有人有 dropwizard jersey 客户端示例吗?

【问题讨论】:

  • 您只是在问如何使用 Jersey Client Api,或者如何以 Dropwizard 特定的方式配置它?如果是前者,可以看Jersey Documentation
  • 我在问如何在 Dropwizard 中使用和配置 Jersey 客户端
  • 要在 Dropwizard 中配置 Jersey 客户端,您应该查看 dropwizard.io/manual/…。要使用,您应该查看@peeskillet 所指向的文档

标签: jersey dropwizard microservices


【解决方案1】:

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>>(){});
    }
}

【讨论】:

  • 所有 DW 链接都返回 404,请问您能修复它们吗? Jersey 的情况也是如此,因为它现在已在 Eclipse 下移动。
【解决方案2】:

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

我的 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;
    }
}

现在,发布网络服务 (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下载。

【讨论】:

    猜你喜欢
    • 2014-12-24
    • 2013-11-17
    • 2015-07-11
    • 2021-08-22
    • 2023-03-13
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多