Dropwizard official documentation 球衣客户端是不可测试的,有人有一个dropwizard球衣客户端样本?
答案 0 :(得分:4)
我发现在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下载。
答案 1 :(得分: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>>(){});
}
}