【发布时间】:2021-04-05 05:29:32
【问题描述】:
我在我的内置 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
【问题讨论】:
-
你是说你想调用一个服务来返回一个
text/html内容类型的响应,你想解析HTML并从HTML的内容创建一个List<Pharmacy>? -
是的。但我想知道我是否可以在没有低级 http 客户端的情况下做到这一点。如果存在某种方式来使用声明式客户端来做到这一点。
-
Micronaut 中没有任何内容可以解析 HTML 并为您创建
List<Pharmacy>。你必须自己写。 -
@JeffScottBrown 使用低级 http 客户端?也许有一个过滤器或转换器来翻译数据。
-
“也许有过滤器或转换器来翻译数据。” - 可以写一个。我们在 micronaut 中不提供。 Micronaut 中没有任何东西可以解析 HTML 有效负载。