'java.lang.IllegalArgumentException: Multi-value reactive types not supported in view resolution: reactor.core.publisher.Flux<MyPojo>

Small question regarding Java + SpringBoot Webflux please.

I have a small handler returning a Flux<MyPojo>

If I build a controller as such (please note the @RestController):

@RestController
public class MyController {

    @PostMapping(path = "/getFluxMyPojo", produces = MediaType.APPLICATION_JSON_VALUE)
    public Flux<MyPojo> getFluxMyPojo() {
        return myPojoService.thisWillGetFluxMyPojo();
    }

}

Things are working fine, very happy, I do get the flux.

What I am having a hard time understanding, if when I change the @RestController to @Controller, as follow:

@Controller
public class MyController {

    @PostMapping(path = "/getFluxMyPojo", produces = MediaType.APPLICATION_JSON_VALUE)
    public Flux<MyPojo> getFluxMyPojo() {
        return myPojoService.thisWillGetFluxMyPojo();
    }

}

Then, I am always getting this exception.

java.lang.IllegalArgumentException: Multi-value reactive types not supported in view resolution: reactor.core.publisher.Flux<MyPojo>
    at org.springframework.web.reactive.result.view.ViewResolutionResultHandler.handleResult(ViewResolutionResultHandler.java:181) ~[spring-webflux-5.3.15.jar:5.3.15]
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 

This is just a web service for other web services to consume, no view or anything. May I ask what is the issue and why am I getting this exception please?

Thank you



Solution 1:[1]

As per Boris mentioned, without @RestController it needs @ResponseBody annotation to tells Spring view resolution is not needed.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 PatPatPat