不存在类型变量 T 的实例,因此 Flux T 不存在.确认 Mono<?扩展 R) [英] No instance(s) of type variable(s) T exist so that Flux&lt;T&gt; confirms to Mono&lt;? extend R)

查看:35
本文介绍了不存在类型变量 T 的实例,因此 Flux T 不存在.确认 Mono<?扩展 R)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am implementing Spring webflux demo application and have written my demo application as like that

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;
}

谁能帮我解决这个错误?

Can anybody help me to solve the error?

推荐答案

在我的代码中完成这些更改后,它起作用了

It works me after i have done these changes in my code

.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;

}

这篇关于不存在类型变量 T 的实例,因此 Flux T 不存在.确认 Mono<?扩展 R)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆