簡體   English   中英

獲取 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 因此,您的服務通過調用 subscribe 發起請求,這將啟動發出請求並將其存儲在數據庫中的流程。

我建議閱讀以下內容

使用@Scheduled的 cron 作業

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

在您訂閱之前什么都不會發生(反應堆文檔)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM