DropWizard/Jersey API客户端

sme*_*eeb 6 java rest jersey webservices-client dropwizard

DropWizard在引擎盖下使用Jersey进行REST.我试图找出如何为我的DropWizard应用程序将公开的RESTful端点编写客户端.

为了这个例子,让我们说我的DropWizard应用程序有一个CarResource,它为CRUDding汽车公开了一些简单的RESTful端点:

@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;
    }
}
Run Code Online (Sandbox Code Playgroud)

所以,我会想象,一个结构化的API客户端会是这样的一个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.
    }
}
Run Code Online (Sandbox Code Playgroud)

但我能找到的只有两个对Drop Wizard客户的官方引用完全相互矛盾:

  • DropWizard推荐的项目结构 - 声称我应该将我的客户端代码放在包car-client下的项目中car.service.client; 但是之后...
  • DropWizard客户端手册 - 使其看起来像"DropWizard客户端",用于将我的DropWizard应用程序与其他 RESTful Web服务集成(因此充当中间人).

所以我想问一下,为DropWizard Web服务编写Java API客户端的标准方法是什么?DropWizard有一个我可以用于此类用例的客户端库吗?我应该通过一些Jersey客户端API实现客户端吗?有人可以添加伪代码给我,CarServiceClient所以我可以理解这将如何工作?

小智 -10

可以与Spring框架集成来实现