Exception in thread "main" java.lang.ClassCastException: class reactor.core.publisher.FluxPeekFuseable cannot be cast to class java.lang.String

Viewed 465

I am learning the Spring Cloud Function. Why should we learn this? I am below error when running the code

Exception in thread "main" java.lang.ClassCastException: class reactor.core.publisher.FluxPeekFuseable cannot be cast to class java.lang.String (reactor.core.publisher.FluxPeekFuseable is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')
    at com.course.kafkaproducer.SpringCloudFunctionApplication.main(SpringCloudFunctionApplication.java:22)

Code

@SpringBootApplication
public class SpringCloudFunctionApplication {

    public static void main(String[] args) {
        //SpringApplication.run(SpringCloudFunctionApplication.class, args);
        
        ApplicationContext context = SpringApplication.run(SpringCloudFunctionApplication.class);
        FunctionCatalog catalog = context.getBean(FunctionCatalog.class);
        Function<String, String> function = catalog.lookup("lowercase");
        System.out.println(function.apply("hello"));
    }
    
    @Bean
    public Function<String, String> uppercase() {
        return value -> value.toUpperCase();
    }
    
    @Bean
    public Function<Flux<String>, Flux<String>> lowercase() {
        return flux -> flux.map(value -> value.toLowerCase());
    }
}
1 Answers

I am not sure what you're trying to do here, but you are invoking reactive function but for some reason expect imperative output, hence your class cast exception which is indeed an expected behavior.

Related