0

我有一个简单的 java 程序,它使用两个服务,我想调用这些服务并结合我收到的结果并应用一些检查。我对spring响应式编程很陌生,所以如果编写的代码有错误,请告诉我

首次服务响应(api/v1/employee/participation)

{
  "empoyeeResponse": {
    "participatedEmployee": [
      {
        "employeeNumber": 1
      },
      {
        "employeeNumber": 2
      }
    ]
  },
  "totalActiveEmployee": 10
}

第二次服务响应(api/v1/employee/:employeeNumber)

{
    "name":"John",
    "age":26,
}

我想制作一项服务,返回超过 30 岁的参与员工列表

{
    "participatedEmployee":[
        {
            "name":"jack",
            "age":33
        }
    ]
}

下面是我的尝试

public serviceClient() {
    this.webClient = WebClient.builder().baseUrl("http://localhost:8080/api/v1")
            .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
            .build();
}


public Mono<EmployeeListResponse> getEmployeeList() {
    return this.webClient.get().uri("/employee/participation")
            .retrieve().bodyToMono(EmployeeListResponse.class);
}

public Mono<EmpolyeeInfo> getEmployeeInfo(Integer id){
    return this.webClient.get().uri("/employee/{id}", id)
            .retrieve().bodyToMono(EmpolyeeInfo.class);
}

public Mono<Void> customService(){
    return this.getEmployeeList()
            .map(EmployeeListResponse::getEmpoyeeResponse)
            .flatMapIterable(EmployeeResponse::getParticipatedEmployee)
            .map(Employee::getEmployeeNumber)
            .flatMap(this::getEmployeeInfo)
            .then();
}

我想更改自定义服务方法的输出以返回 EmpoyeeNewResponse 的 Mono 并申请检查最超过 30 岁的员工,如果不是,则不会将该员工添加到列表中

4

1 回答 1

0

零或一信号

public Mono<List<EmpolyeeInfo>> customService(){
    return this.getEmployeeList()
            .map(EmployeeListResponse::getEmpoyeeResponse)
            .flatMapIterable(EmployeeResponse::getParticipatedEmployee)
            .map(Employee::getEmployeeNumber)
            .flatMap(this::getEmployeeInfo)
            .filter(user -> user.age > 30)
            .collectList();
}

或者

零或 N 信号

public Flux<EmpolyeeInfo> customService(){
    return this.getEmployeeList()
            .map(EmployeeListResponse::getEmpoyeeResponse)
            .flatMapIterable(EmployeeResponse::getParticipatedEmployee)
            .map(Employee::getEmployeeNumber)
            .flatMap(this::getEmployeeInfo)
            .filter(user -> user.age > 30);
}
于 2020-03-18T20:59:48.060 回答