如何解决如何将文本响应绑定到 json 并在 micronaut 上的声明性客户端上放入对象?
我在内置于 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
解决方法
没有成熟的框架 AFAIK 支持 HTML 内容到 POJO
映射(通常在报废中创造),就像 Micronaut,.
同时,您可以轻松地插入基于 jspoon 的转换器 bean,拦截和转换您的 API 结果在等效的 POJO 中:
class Root {
@Selector(value = ".pharmacy") List<Pharmacy> pharmacies;
}
class Pharmacy {
@Selector(value = "span:nth-child(1)") String name;
}
@Client("${services.minsal.url}")
public interface PharmacyClient {
@Get("${services.minsal.path}?${services.minsal.param}=${services.minsal.value}")
Flowable<String> retrieve();
}
@Singleton
public class ConverterService {
public List<Pharmacy> toPharmacies(String htmlContent) {
Jspoon jspoon = Jspoon.create();
HtmlAdapter<Root> htmlAdapter = jspoon.adapter(Root.class);
return htmlAdapter.fromHtml(htmlContent).pharmacies;
}
}
public class StoreService {
private final PharmacyClient pharmacyClient;
private final ConverterService converterService;
public StoreService(PharmacyClient pharmacyClient,ConverterService converterService) {
this.pharmacyClient = pharmacyClient;
this.converterService = converterService;
}
public Flowable<List<Store>> all() {
Flowable<List<Pharmacy>> listFlowable = this.pharmacyClient.retrieve().map(this.converterService::toPharmacies)
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())
)
);
}
}
,
我最终得到了这个。
@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()));
}
}
我仍然想知道我是否可以将数组更改为在声明性客户端中列出。
同时我认为这是一个不错的选择。
更新
我一直都错了。首先,我不需要向 flowable 添加列表,因为当框架公开服务时,它已经响应了一个元素列表。 所以最后我做到了:
@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 可以给我们一些提示。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。