【问题标题】:How to bind text response to json and put in object on a declarative client on micronaut?如何将文本响应绑定到 json 并将对象放在 micronaut 的声明性客户端上?
【发布时间】: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&lt;Pharmacy&gt; ?
  • 是的。但我想知道我是否可以在没有低级 http 客户端的情况下做到这一点。如果存在某种方式来使用声明式客户端来做到这一点。
  • Micronaut 中没有任何内容可以解析 HTML 并为您创建 List&lt;Pharmacy&gt;。你必须自己写。
  • @JeffScottBrown 使用低级 http 客户端?也许有一个过滤器或转换器来翻译数据。
  • “也许有过滤器或转换器来翻译数据。” - 可以写一个。我们在 micronaut 中不提供。 Micronaut 中没有任何东西可以解析 HTML 有效负载。

标签: java http micronaut


【解决方案1】:

没有成熟的框架 AFAIK 支持 HTML 内容到 POJO 映射(通常称为 scraping),例如微航海,.

同时,您可以轻松插入基于 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())
                        )
                );
    }
}

【讨论】:

  • 看起来不错。非常感谢!
  • 请@jics 并且不要忘记接受答案以获得未来的可信度;)
  • 是的。我的意思是这是个好主意,但我不会使用它。事实上,我找到了一种方法,我什至不必手动转换数据。
【解决方案2】:

我最终得到了这个。

@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 可以给我们一些提示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-27
    • 1970-01-01
    • 2012-07-24
    相关资源
    最近更新 更多