Yes, what the dropwizard client provides should only be used by the service itself, most likely to transfer other services. It does not provide anything for client applications directly.
In any case, it does not do magic with HttpClients. It just configures the client according to the yml file, assigns the existing Jackson manipulator object and validator to the Jersey client, and I think it reuses the application thread pool. You can check it all out at https://github.com/dropwizard/dropwizard/blob/master/dropwizard-client/src/main/java/io/dropwizard/client/JerseyClientBuilder.java
I think I will engage in and structure my classes as you did using the Jersey Client. The following is an abstract class that I used for client services:
public abstract class HttpRemoteService { private static final String AUTHORIZATION_HEADER = "Authorization"; private static final String TOKEN_PREFIX = "Bearer "; private Client client; protected HttpRemoteService(Client client) { this.client = client; } protected abstract String getServiceUrl(); protected WebResource.Builder getSynchronousResource(String resourceUri) { return client.resource(getServiceUrl() + resourceUri).type(MediaType.APPLICATION_JSON_TYPE); } protected WebResource.Builder getSynchronousResource(String resourceUri, String authToken) { return getSynchronousResource(resourceUri).header(AUTHORIZATION_HEADER, TOKEN_PREFIX + authToken); } protected AsyncWebResource.Builder getAsynchronousResource(String resourceUri) { return client.asyncResource(getServiceUrl() + resourceUri).type(MediaType.APPLICATION_JSON_TYPE); } protected AsyncWebResource.Builder getAsynchronousResource(String resourceUri, String authToken) { return getAsynchronousResource(resourceUri).header(AUTHORIZATION_HEADER, TOKEN_PREFIX + authToken); } protected void isAlive() { client.resource(getServiceUrl()).get(ClientResponse.class); } }
and here is how I make it specific:
private class TestRemoteService extends HttpRemoteService { protected TestRemoteService(Client client) { super(client); } @Override protected String getServiceUrl() { return "http://localhost:8080"; } public Future<TestDTO> get() { return getAsynchronousResource("/get").get(TestDTO.class); } public void post(Object object) { getSynchronousResource("/post").post(object); } public void unavailable() { getSynchronousResource("/unavailable").get(Object.class); } public void authorize() { getSynchronousResource("/authorize", "ma token").put(); } }
Natan source share