如何将文本响应绑定到 json 并在 micronaut 上的声明性客户端上放入对象?

How to bind text response to json and put in object on a declarative client on 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 映射(通常称为 scraping) 就像 Micronaut, .

的情况一样

与此同时,您可以根据 jspoon 拦截和转换您的 API 结果轻松插入转换器 bean,得到等效的 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 可以给我们一些提示。