Dropwizard official documentation jersey Client isn't testable, someone have a dropwizard jersey client sample?
I found implementing my client in Dropwizard a little bit challenging as well. So I would like to contribute just in case anyone encounters this problem. This is a client in Dropwizard (v1.0.5) that invokes a POST web service using Multipart. The client is accessed via web service as well using GET.
Dependencies in my 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>
Here my Dropwizard Application (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());
}
}
Here my Configuration (Client2PostConfiguration.java):
public class Client2PostConfiguration extends Configuration {
@Valid
@NotNull
private JerseyClientConfiguration jerseyClient = new JerseyClientConfiguration();
@JsonProperty("jerseyClient")
public JerseyClientConfiguration getJerseyClientConfiguration() {
return jerseyClient;
}
}
And now, the post web service (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;
}
}
And finally, the client (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;
}
}
The complete source code can be downloaded from here.