温馨提示:本文翻译自stackoverflow.com,查看原文请点击:java - Spring WebClient Call Two Dependent API

java - Spring WebClient调用两个依赖的API

发布于 2020-04-03 23:30:08

我有两个端点,第一个是to http://localhost:8899/api/composition/appraisal,它将返回所有绩效评估数据

[
    {
        "appraisalId": "ac234fbf-740c-4390-89d4-0e9753ad4d70",
        "employeeId": "emp-4613",
        "grade": "VERY_GOOD",
        "score": 94,
        "status": "NEW"
    },
    {
        "appraisalId": "e9baf663-aa6e-4af8-ae16-ef50c886df28",
        "employeeId": "emp-4623",
        "grade": "VERY_GOOD",
        "score": 94,
        "status": "NEW"
    }
]

我也有另一个端点来获取奖金数据http://localhost:8888/api/composition/bonus/{appraisal_id},该数据根据评估ID返回奖金数据

{
    "appraisalId": "ac234fbf-740c-4390-89d4-0e9753ad4d70",
    "bonusAmount": 4760.0,
    "bonusPaidDate": "2019-06-30",
    "employeeId": "emp-4623",
    "paidToBankAccount": "8240996587"
}

将第一个API的appraisal_id用作参数,我必须将两个API调用的结果合并到PerformanceAppraisalWithBonus.java类中

public class PerformanceAppraisalWithBonus {
    private UUID appraisalId;
    private double bonusAmount;
    private LocalDate bonusPaidDate;
    private String employeeId;
    private String grade;
    private int score;
    private String status;

    getters / setters
}

如何使用Spring WebClient实现此目的?

查看更多

提问者
Timothy
被浏览
34
Raúl Ortiz 2020-02-01 04:43

假设第二个api http://localhost:8888/api/composition/bonus/{appraisal_id}在找不到或不存在id的情况下回复204无内容,然后假设期望的响应是PerformanceAppraisalWithBonus(Flux)的列表,则它将如下所示:

webClient

@Component
public class TestClient {


  public Flux<Appraisal> firstServiceList() {

    return WebClient.create()
            .get()
            .uri("http://demo4307830.mockable.io/first")
            .accept(MediaType.APPLICATION_JSON)
            .retrieve()
            .bodyToFlux(Appraisal.class);

  }

  public Mono<AppraisalBonus> firstServiceFindId(UUID uuid) {

    return WebClient.create()
            .get()
            .uri("http://demo4307830.mockable.io/"+ uuid)
            .accept(MediaType.APPLICATION_JSON)
            .retrieve()
            .bodyToMono(AppraisalBonus.class);

  }
}

服务

@Service
public class AppraisalServiceImpl {

  @Autowired
  private TestClient testClient;

  public Flux<AppraisalCombine> combineTwo() {
    return testClient.firstServiceList()
            .flatMap(appraisal -> testClient.firstServiceFindId(appraisal.getAppraisalId())
                    .flatMap(appraisalBonus -> Mono.just(AppraisalCombine.builder()
                            .appraisalId(appraisalBonus.getAppraisalId())
                            .bonusAmount(appraisalBonus.getBonusAmount())
                            .bonusPaidDate(appraisalBonus.getBonusPaidDate())
                            .employeeId(appraisalBonus.getEmployeeId())
                            .grade(appraisal.getGrade())
                            .score(appraisal.getScore())
                            .status(appraisal.getStatus())
                            .build())));

  }
}

控制者

@RestController
@RequestMapping("/appraisal")
public class AppraisalController {

  @Autowired
  AppraisalServiceImpl appraisalService;

  @GetMapping(value = "/get")
  public Flux<AppraisalCombine> uploadSimple() {
    return appraisalService.combineTwo();
  }
}

结果

[
    {
        "appraisalId": "ac234fbf-740c-4390-89d4-0e9753ad4d70",
        "bonusAmount": 4760.0,
        "bonusPaidDate": "2019-06-30",
        "employeeId": "emp-4623",
        "grade": "VERY_GOOD",
        "score": 94,
        "status": "NEW"
    }
]

发布
问题

分享
好友

手机
浏览

扫码手机浏览