Я вижу, что происходит что-то интересное.Это работает, как и ожидалось, если я управляю этим из класса 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()));
}
}