ホーム>source

サーバー送信イベントに取り組んでいます。私は助けを借りました

http://sinhamohit.com/writing/spring-boot-reactive-sse

https://github.com/mohitsinha/spring-boot-reactive-sse

上記の例の問題は、すべてが1つのクラスで定義されていることです。私はそれを別のクラスにしようとしていますが、例外で失敗します:

2018-08-20 17:03:15.521  WARN 10964 --- [           main] 
onfigReactiveWebServerApplicationContext : Exception encountered during 
context initialization - cancelling refresh attempt: 
org.springframework.context.ApplicationContextException: Unable to start 
reactive web server; nested exception is 
org.springframework.context.ApplicationContextException: Unable to start 
ReactiveWebApplicationContext due to missing ReactiveWebServerFactory bean.
2018-08-20 17:03:15.599 ERROR 10964 --- [           main] 
o.s.boot.SpringApplication               : Application run failed
org.springframework.context.ApplicationContextException: Unable to start 
reactive web server; nested exception is 
org.springframework.context.ApplicationContextException: Unable to start 
ReactiveWebApplicationContext due to missing ReactiveWebServerFactory bean.
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.onRefresh(ReactiveWebServerApplicationContext.java:76) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:61) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:330) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1258) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at hello.SpringBootApplication.main(SpringBootApplication.java:8) [classes/:na]

コード:

   <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.4.RELEASE</version>
    </parent>
   <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
   </dependencies>

   @RestController
   @RequestMapping("/stock/transaction")
   public class StockTransactionController {
       @Autowired
       StockTransactionService stockTransactionService;
       @GetMapping(produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
       public Flux<StockTransaction> stockTransactionEvents() {
           return stockTransactionService.getStockTransactions();
      }
   }
   @Service
   public class StockTransactionService {
     List<Stock> stockList = new ArrayList<>();
     List<String> stockNames = 
          Arrays.asList("mango,banana,guava,infinity".split(","));
     @Bean
     CommandLineRunner commandLineRunner() {
        return args -> {
        createRandomStock();
        stockList.forEach(System.out::println);
     };
     }
    public Flux<StockTransaction> getStockTransactions() {
    Flux<Long> interval = Flux.interval(Duration.ofSeconds(1));
    interval.subscribe((i) -> stockList.forEach(stock -> 
    stock.setPrice(changePrice(stock.getPrice()))));
    Flux<StockTransaction> stockTransactionFlux = Flux
            .fromStream(Stream.generate(() -> new 
    StockTransaction(getRandomUser(), getRandomStock(), new Date())));
    return Flux.zip(interval, stockTransactionFlux).map(Tuple2::getT2);
   }
   }

助けてください。

あなたの答え
  • 解決した方法 # 1

    問題は注釈にありました。 @SpringBootApplication

関連記事

  • 前へ java - JPAクエリ:サブクエリをグループ化条件に結合する
  • 次へ python - ifステートメントの構文