没有类型变量T的实例存在以使Flux 确认Mono延伸R)

问题描述 投票:0回答:2

我正在实现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)))

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
2个回答
2
投票

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

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

Event.Class中的@NoArgsConstructor注释

import java.util.Date;

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

}

0
投票

flatMapMany而不是flatMap的工作原理。

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

© www.soinside.com 2019 - 2024. All rights reserved.