【问题标题】:No instance(s) of type variable(s) T exist so that Flux<T> confirms to Mono<? extend R)不存在类型变量 T 的实例,因此 Flux<T> 确认 Mono<?扩展 R)
【发布时间】:2018-09-14 11:51:22
【问题描述】:

我正在实现 Spring webflux 演示应用程序,并且已经编写了我的演示应用程序

package com.abcplusd.application;

import com.abcplusd.domain.Event;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;

import java.util.Collections;
import java.util.Date;
import java.util.stream.Stream;

@SpringBootApplication
public class ReactiveClientApplication {

    @Bean WebClient webClient() {
        return WebClient.create("http://localhost:8080");
    }

    @Bean CommandLineRunner demo(WebClient webClient) {
        return args -> {
            webClient.get()
                .uri("/events")
                .accept(MediaType.TEXT_EVENT_STREAM)
                .exchange()
                .flatMap(clientResponse -> clientResponse.bodyToFlux(Event.class))
                .subscribe(System.out::println);
        };
    }
    public static void main(String[] args) {
        new SpringApplicationBuilder(ReactiveClientApplication.class)
            .properties(Collections.singletonMap("server.port", "8081"))
            .run(args);
    }
}

显示如下错误

Error:(29, 41) java: incompatible types: no instance(s) of type variable(s) T exist so that reactor.core.publisher.Flux<T> conforms to reactor.core.publisher.Mono<? extends R>

上面的错误在这一行:

                    .flatMap(clientResponse -> clientResponse.bodyToFlux(Event.class)))

事件类

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.Date;

@Data
@AllArgsConstructor
public class Event {
    private  long id;
    private Date when;
}

谁能帮我解决这个错误?

【问题讨论】:

    标签: spring-boot spring-webflux react-native-router-flux


    【解决方案1】:

    在我对我的代码进行这些更改后,它对我有用

    .flatMap(clientResponse -> clientResponse.bodyToFlux(Event.class))) 
    

    .flatMapMany(clientResponse -> clientResponse.bodyToFlux(Event.class))
    

    @NoArgsConstructor annotation in Event.Class
    

    如下:

    import java.util.Date;
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Event {
        private  long id;
        private Date when;
    
    }
    

    【讨论】:

      【解决方案2】:

      flatMapMany 代替 flatMap 有效。

      但是,当您将属性 spring.main.web_environment=false 添加到 application.properties 文件时,webClient 根本不起作用。

      【讨论】:

        猜你喜欢
        • 2017-04-22
        • 2020-11-04
        • 2019-12-16
        • 1970-01-01
        • 2020-07-13
        • 1970-01-01
        • 2017-12-04
        • 1970-01-01
        • 2018-07-31
        相关资源
        最近更新 更多