1

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());
    }
}
PAA
  • 1
  • 46
  • 174
  • 282

1 Answers1

-1

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.

Oleg Zhurakousky
  • 5,820
  • 16
  • 17
  • Can you share some code to start of leaning Spring Cloud Function? Because readme are not clear hence having issues in understanding things for beginner. Please do the needful. – PAA May 16 '21 at 16:18
  • Again, I am not sure what 'readme' you are referring to when you say it is not clear, so please share it so we can make it clear. That said, the samples are available here - https://github.com/spring-cloud/spring-cloud-function/tree/main/spring-cloud-function-samples Also, here is the link to Programming Model section of the docs - https://docs.spring.io/spring-cloud-function/docs/3.0.14.RELEASE/reference/html/spring-cloud-function.html#_programming_model which provides significant amount tof detail – Oleg Zhurakousky May 17 '21 at 03:16