DropWizard使用Jersey爲REST引擎蓋下。我想弄清楚如何爲我的DropWizard應用程序將公開的RESTful端點編寫客戶端。DropWizard/Jersey API客戶端
對於這個例子的目的,讓我們說我DropWizard應用程序有一個CarResource
,這暴露了幾個簡單的RESTful端點CRUDding汽車:
@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 recommended project structure - 聲稱我應該把我的客戶端代碼在
car-client
項目下car.service.client
包;但後來... - DropWizard Client manual - 這使得它看起來像一個「DropWizard客戶端」是用於集成我的DropWizard應用程序與其他 REST風格的Web服務(因此充當中間人)。
所以我問,爲您的DropWizard Web服務編寫Java API客戶端的標準方式是什麼? DropWizard有一個客戶端庫,我可以利用這種類型的用例嗎?我應該通過一些Jersey客戶端API來實現客戶端嗎?有人可以添加僞代碼到我的CarServiceClient
,所以我可以理解這將如何工作?