4

We have a large codebase which uses SpringMVC:

            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.6.RELEASE</version>

Now we've faced a challenge to integrate reactive code into on of controllers: the API of external library accepts and provides Publisher<ByteByffer> for huge binary blob. We decided to use WebFlux for that, and created a method in controller to use with it:

     <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>

and controller:

    @RequestMapping("/.../some/path/")
    public CompletionStage<ResponseEntity<Flux<DataBuffer>>> handle(RequestEntity<Flux<DataBuffer>> req) {
         // processing and returning of blobs
    }

It accepts request publisher successfully, but response is generated not correctly: the response is returned as a JSON with field nativeBuffer with base64 encoded binary data like in this ticket: https://github.com/spring-projects/spring-framework/issues/19714

As I understand from Can I use SpringMvc and webflux together? answer, it's not supposed to use SpringMVC and WebFlux together, but we can't migrate all our 100+ controllers to Flux in one step. Is it possible to move iteratively and migrate them one by one, keeping MVC controllers and Flux in a same project?

Kirill
  • 51
  • 3
  • 1
    Thats because you are Returning a `CompletionState` object containing your response. Why are you even wrapping the response, a response, is what it is it is a Rest, response object. How can spring know that what you want serialized is the `DataBuffer` in the end. What it does now is to serialize The `ResponseEntity` and assuming that the `Flux` is just an object that needs to be serialized, and not a stream of bytes. Try returning just a `ResponseEntity>` or just a `Flux` or a `ServerResponse` and my guess it will go a lot better. – Toerktumlare Feb 04 '21 at 09:04
  • @Toerktumlare thanks for the answer, the `ResponseEntity` is used because we need to attach some dynamic headers in runtime to the response depends on data returned; `CompletionState` is used because the API of library we're using is asynchronous and doesn't returns `Publisher` immediately. Is it possible to add some hint for Spring about returned type of data? – Kirill Feb 04 '21 at 09:08
  • try converting it to a `Mono` using `Mono#fromCompletionStage` – Toerktumlare Feb 04 '21 at 09:14

0 Answers0