micronautに組み込まれているアプリで宣言型httpクライアントを作成しました。これは、text / htmlタイプで応答するサービスを消費する必要があります。
私はなんとかリストを手に入れることができましたが、LinkedHashMap
中にはあります。そして私はそれらをの対象にしたいPharmacy
私の質問は、その応答をオブジェクトのリストに変換するにはどうすればよいですか?
@Client("${services.url}")
public interface PharmacyClient {
@Get("${services.path}?${services.param}=${services.value}")
Flowable<List<Pharmacy>> retrieve();
}
public class StoreService {
private final PharmacyClient pharmacyClient;
public StoreService(PharmacyClient pharmacyClient) {
this.pharmacyClient = pharmacyClient;
}
public Flowable<List<Store>> all() {
Flowable<List<Pharmacy>> listFlowable = this.pharmacyClient.retrieve();
return listFlowable
.doOnError(throwable -> log.error(throwable.getLocalizedMessage()))
.flatMap(pharmacies ->
Flowable.just(pharmacies.stream() // here is a list of LinkedHashMap and i'd like to user Pharmacy objects
.map(pharmacy -> Store.builder().borough(pharmacy.getBoroughFk()).build())
.collect(Collectors.toList())
)
);
}
}
コード:https://github.com/j1cs/drugstore-demo/tree/master/backend
私はこれで終わった。
@Client("${services.url}")
public interface PharmacyClient {
@Get(value = "${services.path}?${services.param}=${services.value}")
Flowable<Pharmacy[]> retrieve();
}
public class StoreService {
private final PharmacyClient pharmacyClient;
public StoreService(PharmacyClient pharmacyClient) {
this.pharmacyClient = pharmacyClient;
}
public Flowable<List<Store>> all() {
Flowable<Pharmacy[]> flowable = this.pharmacyClient.retrieve();
return flowable
.switchMap(pharmacies ->
Flowable.just(Arrays.stream(pharmacies)
.map(pharmacyStoreMapping)
.collect(Collectors.toList())
)
).doOnError(throwable -> log.error(throwable.getLocalizedMessage()));
}
}
それでも、宣言型クライアントでリストに配列を変更できるかどうかを知りたいです。
一方、これは良いオプションだと思います。
私はずっと間違っていました。まず、フレームワークがサービスを公開すると、すでに要素のリストで応答するため、フローアブルにリストを追加する必要はありません。だから最後に私はこれをしました:
@Client("${services.url}")
public interface PharmacyClient {
@Get(value = "${services.path}?${services.param}=${services.value}")
Flowable<Pharmacy> retrieve();
}
public class StoreService {
private final PharmacyClient pharmacyClient;
public StoreService(PharmacyClient pharmacyClient) {
this.pharmacyClient = pharmacyClient;
}
public Flowable<Store> all() {
Flowable<Pharmacy> flowable = this.pharmacyClient.retrieve();
return flowable
.switchMap(pharmacyPublisherFunction)
.doOnError(throwable -> log.error(throwable.getLocalizedMessage()));
}
ご覧のとおり、httpクライアントはtext / htmlデータを自動的にjsonに変換し、それを適切に解析します。なぜ本当になのかわかりません。たぶん@JeffScottBrownは私たちにいくつかのヒントを与えることができます。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句