DropWizard在REST方面使用Jersey。我正在尝试弄清楚如何为我的DropWizard应用程序编写RESTful端点的客户端。
为了举例说明,假设我的DropWizard应用程序有一个CarResource
,它公开了一些简单的RESTful端点以CRUD汽车:
@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;
}
}
所以我想象一个结构化的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.
}
}
但是我能找到的唯二关于DropWizard客户端的官方参考资料彼此完全相反:
- DropWizard 推荐项目结构 - 声称我应该将客户端代码放在
car.service.client
包下的car-client
项目中;但是... - DropWizard 客户端手册 - 看起来好像 "DropWizard 客户端" 是用于将我的 DropWizard 应用程序与其他 RESTful Web 服务集成(从而充当中间人)。
CarServiceClient
添加伪代码,以便我了解如何工作吗?