[英]How to call an external API with vertx and return data to java object
我正在嘗試創建一個調用外部API並使用Java模型獲取數據的vertx客戶端。
我想做的一個例子是,下面只是一個粗略的工作
WebClient client = WebClient.create(vertx);
// Send a GET request
client
.get(8080, "mytour.mycompany.com", "/getRusult?pricemin=0&priceMax=1000&beach=true....etc")
.send(ar -> {
if (ar.succeeded()) {
// Obtain response
HttpResponse<Buffer> response = ar.result();
mytour是一個外部API,我想將從調用中獲得的數據返回到已經為此設計的java類中。 為了簡潔起見,下面的Java類並未完全顯示,但它遵循構建器模式原則。 我想要從API調用獲得的數據到此類。
public class Tour {
private Departure departure;
private Arrival arrival;
private TourType tourType;
private Hotel hotel;
private Beach beach;
private Tour() {
}
我不確定如何解決這個問題。 在這一點上,這是我在其他方法中從Buffer獲取游覽對象的方法,該怎么做?
public Tour clientTest() {
Vertx vertx = Vertx.vertx();
WebClient client = WebClient.create(vertx);
// Send a GET request
client
.get("https://www.mytour.com/tariffsearch/getResult?" +
"priceMin=0&priceMax=1500000¤cy=533067&nightsMin=6&nightsMax=8" +
"&hotelClassId=269506&accommodationId=2&rAndBId=15350&tourType=1&" +
"locale=ru&cityId=786&countryId=1104&after=01.08.2018&before=01.08.2018&" +
"hotelInStop=false&specialInStop=false&version=2&tourId=1285&" +
"tourId=12689&tourId=12706&tourId=143330&tourId=9004247&" +
"tourId=4433&tourId=5736&tourId=139343&tourId=4434&tourId=12691&" +
"tourId=21301&tourId=12705&tourId=149827&tourId=4151426&hotelClassBetter=true&" +
"rAndBBetter=true&noTicketsTo=false&noTicketsFrom=false&searchTypeId=3&" +
"recommendedFlag=false&salePrivateFlag=false&onlineConfirmFlag=false&contentCountryId=1102")
.send(ar -> {
if (ar.succeeded()) {
// Obtain response
HttpResponse<Buffer> response = ar.result();
Tour tour = (Tour) response.body();
}
});
}
我想將API調用中的數據返回給我的Tour類
而不是像這樣將response.body()
轉換為Tour
類型:
HttpResponse<Buffer> response = ar.result();
Tour tour = (Tour) response.body();
您應該將body
轉換為對象( POJO
)。 您可以通過至少幾種方式來做到這一點。 首先,在這里查看Vert.x HttpResponse
類的JavaDoc: https ://vertx.io/docs/apidocs/io/vertx/ext/web/client/HttpResponse.html。 我們有例如以下方法: response.bodyAsString()
。 一旦我們將響應主體作為String
並假設此String
包含JSON數據格式的響應,就可以將該String
轉換為對象,例如使用Gson庫或其他解決方案。
例如:
HttpResponse<Buffer> response = ar.result();
String bodyAsString = response.bodyAsString();
Gson gson = new Gson();
Tour tour = gson.fromJson(bodyAsString, Tour.class);
之后,當Tour
對象與JSON HTTP響應兼容時,您應該將響應數據映射到Tour
對象中。
我不知道您的HTTP響應的結構。 如果Tour
對象與HTTP響應不兼容,則應創建另一個對象-例如TourResponse
,它將與此響應兼容,並創建一個映射器,該映射器會將TourResponse
轉換為Tour
對象。
我想我了解您的要求,並且在大多數情況下,我會使用響應式方法來進行呼叫。 這樣做的原因是要像處理數據一樣處理數據,而不是處理潛在的嵌套回調。
示例Kotlin代碼:
webClient.get(PORT, URL)
.rxSend()
//convert to observable stream
.toObservable()
//get responsebody as a jsonarray of objects
.map { response: HttpResponse<Buffer> ->
response.bodyAsJsonArray().toMutableList() as List<JsonObject>}
//segment the array into a stream of single object
.flatMap { objs: List<JsonObject> -> Observable.fromIterable(objs)}
//convert each object the json object stream into a java object
.map {entry:JsonObject -> Json.decode(entry, Tour.javaClass)}
執行此代碼后,您將擁有一個Observable<Tour>
,可以對它進行流式處理並在Tour Objects上進行任何處理,或者可以僅調用toList()
轉換為Single<List<Tour>>
並在訂閱中方法句柄將響應返回給調用者。
如果您有任何疑問,請通知我!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.