我看到一些有趣的事情正在发生。如果我从 Controller 类编排它,它会按预期工作,而如果我从我的 Controller 类调用一个服务来编排这个流,它似乎不会按预期工作。只是想知道我错过了什么?或者这就是它的工作方式?
这是工作代码:
@RestController
@RequestMapping("/applications")
@Slf4j
@RequiredArgsConstructor
public class ApplicationController {
private final ApplicationService applicationService;
private final ApplicationRequestMapper requestMapper;
private final FeesService feesService;
@PostMapping(value = "/save")
public Mono<Application> saveApplication(@RequestBody ApplicationRequest request) {
ApplicationRequest applicationRequest = requestMapper.apply(request);
return Mono.subscriberContext()
.flatMap(context -> feesService.calculateApplicationFees(applicationRequest)
.collectList())
.map(feeItems -> applicationRequest.getFeeItems().addAll(feeItems))
.flatMap(isRequestEnriched -> applicationService.saveApplication(applicationRequest)
.map(saveApplicationResponse -> {
Application application = new Application();
application.setLicenceId(saveApplicationResponse.getResponse().getLicenceNumber());
return application;
}))
.onErrorMap(throwable -> new ApplicationException(String.format(SAVE_ERROR_MESSAGE,
request.getLicenceId()),
throwable, true, false))
.log();
}
}
@Service
@Slf4j
@RequiredArgsConstructor
public class ApplicationService extends ClientService{
private final AuthenticationService authenticationService;
public Mono<SaveApplicationResponse> saveApplication(ApplicationRequest request) {
return Mono.subscriberContext()
.flatMap(context -> authenticationService.getAccessToken()
.flatMap(token -> post("/save",
request,
token.getAccessToken(),
context)
.bodyToMono(SaveApplicationResponse.class))
.log());
}
}
@Service
@Slf4j
@RequiredArgsConstructor
public class FeesService extends ClientService{
private final AuthenticationService authenticationService;
public Flux<FeeItem> calculateApplicationFees(ApplicationRequest request) {
return Mono.subscriberContext()
.flatMap(ctx -> authenticationService.getAccessToken()
.flatMap(token -> get("/fees", request, token.getAccessToken(), ctx)
.bodyToMono(FeeResponse.class))
.log())
.flatMapMany(rsp -> Flux.fromIterable(rsp.getFeeItems()));
}
}
如果我这样做就不起作用..意思是,请求永远不会丰富:
@RestController
@RequestMapping("/applications")
@Slf4j
@RequiredArgsConstructor
public class ApplicationController {
private final ApplicationService applicationService;
private final ApplicationRequestMapper requestMapper;
@PostMapping(value = "/save")
public Mono<Application> saveApplication(@RequestBody ApplicationRequest request) {
return Mono.subscriberContext()
.flatMap(context -> applicationService.saveApplication(requestMapper.apply(request))
.map(saveApplicationResponse -> {
Application application = new Application();
application.setLicenceId(saveApplicationResponse.getResponse().getLicenceNumber());
return application;
}))
.onErrorMap(throwable -> new ApplicationException(String.format(SAVE_ERROR_MESSAGE,
request.getLicenceId()),
throwable, true, false))
.log();
}
}
@Service
@Slf4j
@RequiredArgsConstructor
public class ApplicationService extends ClientService{
private final AuthenticationService authenticationService;
private final FeesService feesService;
public Mono<SaveApplicationResponse> saveApplication(ApplicationRequest request) {
return Mono.subscriberContext()
.flatMap(context -> feesService.calculateApplicationFees(request)
.collectList())
.map(feeItems -> request.getFeeItems().addAll(feeItems))
.subscriberContext()
.flatMap(context -> authenticationService.getAccessToken()
.flatMap(token -> post("/save",
request,
token.getAccessToken(),
context)
.bodyToMono(SaveApplicationResponse.class))
.log());
}
}
@Service
@Slf4j
@RequiredArgsConstructor
public class FeesService extends ClientService{
private final AuthenticationService authenticationService;
public Flux<FeeItem> calculateApplicationFees(ApplicationRequest request) {
return Mono.subscriberContext()
.flatMap(ctx -> authenticationService.getAccessToken()
.flatMap(token -> get("/fees", request, token.getAccessToken(), ctx)
.bodyToMono(FeeResponse.class))
.log())
.flatMapMany(rsp -> Flux.fromIterable(rsp.getFeeItems()));
}
}