DropWizard / Jersey API Clients

DropWizard uses Jersey under the hood for REST. I am trying to figure out how to write a client for the RESTful endpoints that my DropWizard application will open.

For this example, let's say my DropWizard application has CarResource , which provides some simple RESTful endpoints for CRUDding cars:

 @Path("/cars") public class CarResource extends Resource { // CRUDs car instances to some database (DAO). public CardDao carDao = new CarDao(); @POST public Car createCar(String make, String model, String rgbColor) { Car car = new Car(make, model, rgbColor); carDao.saveCar(car); return car; } @GET @Path("/make/{make}") public List<Car> getCarsByMake(String make) { List<Car> cars = carDao.getCarsByMake(make); return cars; } } 

So, I would suggest that a structured API client would be something like CarServiceClient :

 // Packaged up in a JAR library. Can be used by any Java executable to hit the Car Service // endpoints. public class CarServiceClient { public HttpClient httpClient; public Car createCar(String make, String model, String rgbColor) { // Use 'httpClient' to make an HTTP POST to the /cars endpoint. // Needs to deserialize JSON returned from server into a `Car` instance. // But also needs to handle if the server threw a `WebApplicationException` or // returned a NULL. } public List<Car> getCarsByMake(String make) { // Use 'httpClient' to make an HTTP GET to the /cars/make/{make} endpoint. // Needs to deserialize JSON returned from server into a list of `Car` instances. // But also needs to handle if the server threw a `WebApplicationException` or // returned a NULL. } } 

But only the two official links to Drop Wizard clients that I can find are completely contradictory:

So, I ask, what is the standard way to write Java API clients for your DropWizard web services? Does DropWizard have a client library that I can use for this type of use? Should I deploy a client through the Jersey client API? Can someone add pseudo code to my CarServiceClient so that I can understand how this will work?

+6
source share
4 answers

Here is a template that you can use with the JAX-RS client.

To get a customer:

 javax.ws.rs.client.Client init(JerseyClientConfiguration config, Environment environment) { return new JerseyClientBuilder(environment).using(config).build("my-client"); } 

You can make calls as follows:

 javax.ws.rs.core.Response post = client .target("http://...") .request(MediaType.APPLICATION_JSON) .header("key", value) .accept(MediaType.APPLICATION_JSON) .post(Entity.json(myObj)); 
+1
source

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

if someone tries to use DW 0.8.2 when creating a client, and you get the following error:

 cannot access org.apache.http.config.Registry class file for org.apache.http.config.Registry not found at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:858) at org.apache.maven.plugin.compiler.CompilerMojo.execute(CompilerMojo.java:129) at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208) ... 19 more 

update your dropwizard-client in your pom.xml from 0.8.2 to 0.8. 4 , and you should be fine. I believe the additive sub-dependency has been updated, which fixed it.

  <dependency> <groupId>io.dropwizard</groupId> <artifactId>dropwizard-client</artifactId> <version>0.8.4</version> <scope>compile</scope> </dependency> 
0
source

You can integrate with Spring Framework to implement

-ten
source

Source: https://habr.com/ru/post/977389/


All Articles