当前位置: 首页 > 知识库问答 >
问题:

使用WebClient/Spring Boot2将REST响应映射到单个的正确方法

裴劲
2023-03-14
@ApiOperation(value = "getRelatedAccounts")
@GetMapping(value = "/relatedaccounts")
public ResponseEntity<RelatedAccounts> getRelatedAccounts(@RequestParam String accountNumber) {
    return new ResponseEntity<>(myService.getRelatedAccounts(accountNumber), HttpStatus.OK);
}
public RelatedAccounts getRelatedAccounts(String accountNumber) {
    // blah blah, hit the endpoint and get my response of a list of account numbers in relatedAccountsList
    Flux<AccountInformation> accountInfoFlux = Flux.fromIterable(relatedAccountsList)
        .parallel()
        .runOn(Schedulers.elastic())
        .flatMap(this::getAccountInformation)
        .ordered(Comparator.comparing(RelatedAccounts::getCorrectOrder)) // blah blah, convert from ParallelFlux to Flux
}
public Mono<AccountInformation> getAccountInformation(String accountNumber) {
    WebClient webClient = WebClient.builder()
            .baseUrl("http://myurl.com")
            .build();

    return webClient
            .get()
            .uri(uriBuilder -> uriBuilder
                    .queryParam("accountNumber", accountNumber)
                    .build()
            ).retrieve()
            .bodyToMono(AccountInformation.class) // This doesn't work for us, we get a very complex object as a response and need to parse a few fields.
}

我花了一整天在谷歌,我没有看到其他人解析他们得到的响应,他们只是神奇地将它直接映射到一个完美创建的类上。我们没有这个选项,我们需要从响应体中提取accountName并将其放在AccountInformation对象中。有人知道怎么做吗?

共有1个答案

李华茂
2023-03-14

我的建议是使用bodytomono(accountinformation.class),然后使用Monos map和zip映射到您的简单对象。

  1. 创建一个表示复杂AccountInformation的类,但只包含您需要的信息(不要包含您不需要的对象字段)。
  2. 使用其他服务的确切代码(返回Mono)
  3. 可以使用.zip将简单对象与AccountInformation对象组合。
  4. 因此,简单对象只有复杂对象所需的数据

如。

其他服务保持不变:

public Mono<AccountInformation> getAccountInformation(String accountNumber) {
    WebClient webClient = WebClient.builder()
            .baseUrl("http://myurl.com")
            .build();

    return webClient
            .get()
            .uri(uriBuilder -> uriBuilder
                    .queryParam("accountNumber", accountNumber)
                    .build()
            ).retrieve()
            .bodyToMono(AccountInformation.class)
}

创建缺少的类(表示其他服务的结果),跳过所有不需要的Info字段

import java.util.List;

public class AccountInformation {
    private String infoYouWant;
    private List<AccountDetails> otherNestedInfoYouWant;
// getters/setters skipped for easier read
}
        Mono<AccountInformation> accountInformation = someService.getAccountInformation("fillMeWithAccountNumber");
        Mono<SimpleObject> simpleObjectWithAccountInformation = Mono.zip(simpleObject, accountInformation).map(objects -> {
            SimpleObject simpleObject = objects.getT1();
            simpleObject.setAccountNumber(objects.getT2().getInfoYouWant());
            return simpleObject;
        });
 类似资料:
  • 假设您希望通过调用到来获取用户列表,但当前该表被截断,因此没有用户。对于这种情况,正确的反应是什么:或?

  • 我想在Spring中创建一个符合REST的endpoint。endpoint应该允许分页。 请求非常简单,是否访问url/资源?页码=1 我发现了两种返回数据的大方法: 返回包含以下内容的自定义对象: 此方法的问题是它不再返回资源,而是返回资源的包装器。 这解决了资源问题,但它使endpoint更难使用,这是违反直觉的,因为它也需要额外的工作来实现。 我想知道,什么标准被更广泛地使用,为什么?对此

  • 在进行web客户端调用并使用前一个Mono的响应后,我无法在映射中获得填充值。以下是我所尝试的代码。parameters.size()的值为零。无法获得该值未被文件化的原因。我基本上希望从该方法返回年龄(而不是单对象)。使用block给出一个错误block()/blockfirst()/blocklast()是blocking,这在thread Reactor-http-nio-3中是不支持的。

  • 我希望有人能帮我解决这个问题。 我想用WebClient创建一个Rest客户端,从API中检索响应。所以我创建了我的Spring项目,添加了webflux、lombok和h2。我还创建了一个DTO类“CashAccount”和以下方法: 当我使用“.bodyToMono(String.class)”时,所有的功能都很好,我收到了结果: 相反,当我使用“.bodyToMono(cashcount.c

  • 什么是REST API响应结构和布局的最佳实践? Scrath示例: 成功回应: 失败响应:

  • 我有4张桌子: 库存(库存ID、可用数量) 一个客户可以有多个订单,而一个订单由多个orderDetails项目组成。我正在尝试将库存项目存储在由库存项目和数量整数组成的映射中。 当我尝试以这种方式持久化它时,第一个库存项目将以1的orderID添加到Order详细信息表中。但是下一个是使用2的orderID插入的(不存在)。 有什么帮助吗?