获取 NoSuchBeanDefinitionException:Spring WebFlux 中没有类型为 ServerRequest 的符合条件的 bean

Getting NoSuchBeanDefinitionException: No qualifying bean of type ServerRequest in Spring WebFlux

由于我仍然没有为我的主题找到一个完全令人满意的响应式解决方案:click,主要假设是:

How to run a Web Flux method cyclically in a reactive way?

我找到了一个使用 @Scheduled 注释使其成为现实的解决方法。实施如下:

@Component
@AllArgsConstructor
public class Covid19APIHandler {

  private Covid19APIService apiService;

  private CountryCasesHistoryRepository repository;

  private CountryCasesWrapperRepository countryCasesWrapperRepository;

  private ServerRequest serverRequest;

  public Mono<Void> getCountryCasesAndSave(ServerRequest serverRequest) {
    return apiService
        .findCasesByCountry()
        .flatMap(
            wrapper ->
                countryCasesWrapperRepository
                    .save(
                        CountryCasesWrapper.builder()
                            .countries_stat(wrapper.getCountries_stat())
                            .statistic_taken_at(wrapper.getStatistic_taken_at())
                            .build())
                    .then(Mono.empty()));
  }

  @Scheduled(fixedDelay = 10000)
  public void casesByCountryScheduled() {
    getCountryCasesAndSave(serverRequest);
  }
}

问题是在执行代码时我收到一个错误:

Description:

Parameter 3 of constructor in com.covid.application.Covid19APIHandler required a bean of type 'org.springframework.web.reactive.function.server.ServerRequest' that could not be found.

Action:

Consider defining a bean of type 'org.springframework.web.reactive.function.server.ServerRequest' in your configuration.

我用 @RequiredArgsConstructor 尝试了 final 关键字,生成了所有参数。通过 IntelliJ 构造函数,但我的 ServerRequest 字段仍未初始化。问题来了,如何创建我的 ServerRequest 自定义 bean 并使其正确初始化。我将不胜感激关于如何让它成为现实的建议。

正如评论中所指出的,为什么您甚至需要 ServerRequest,您甚至都没有使用它。删除它并清理代码。

@Component
public class Covid19APIHandler {

    private Covid19APIService apiService;
    private CountryCasesWrapperRepository countryCasesWrapperRepository;

    @Autowire
    public Covid19APIHandler(Covid19APIService apiService, CountryCasesHistoryRepository repository) {
        this.apiService = apiService;
        this.repository = repository;
    }

    @Scheduled(fixedDelay = 10000)
    public void casesByCountryScheduled() {
        apiService.findCasesByCountry()
            .flatMap(response ->
                return countryCasesWrapperRepository.save(
                    CountryCasesWrapper.builder()
                        .countries_stat(response.getCountries_stat())
                        .statistic_taken_at(response.getStatistic_taken_at())
                        .build()))
        .subscribe();
    }
}

在这段代码中,您的计划任务将 运行 根据 fixedDelay 如果您希望它每小时 运行 我建议设置一个 cron 作业调度程序。

代码将 运行 因为 subscribe。您会看到,当您调用 subscribe 时,您基本上是在 运行 编译代码。 consumer 应该始终是 subscriber。您的应用程序是另一个应用程序的 consumer。因此,您的服务通过调用订阅发起请求,这将开始发出请求并将其存储在您的数据库中的流程。

我建议阅读以下内容

cron 作业 @Scheduled

https://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/scheduling.html#scheduling-annotation-support-scheduled

Cron 语法

https://en.wikipedia.org/wiki/Cron

在您订阅之前什么都不会发生(reactor 文档)

https://projectreactor.io/docs/core/release/reference/#reactive.subscribe