2

Spring MVC controller is to be used as a proxy for another service to serving possibly large files.

To avoid storing the whole WebClient response from another service in memory at any given time I wanted to use reactive properties of Spring 5 + Reactor project and stream its response as it's consumed by the browser.

The current code looks like

import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.reactive.function.client.ClientResponse
import org.springframework.web.reactive.function.client.WebClient
import reactor.core.publisher.Mono

@Controller
class ReportController {

    @GetMapping("/reports")
    fun getReport(): Mono<ClientResponse> {
        val webCient = WebClient.create()
        return webCient
                .get()
                .uri("http://localhost:3333/file")
                .exchange()
    }
}

And it causes

java.lang.IllegalStateException: Could not resolve view with name 'reports'.
    at org.springframework.web.reactive.result.view.ViewResolutionResultHandler.lambda$resolveViews$3(ViewResolutionResultHandler.java:277) ~[spring-webflux-5.0.6.RELEASE.jar:5.0.6.RELEASE]
  • How to configure the controller to handle Mono<ClientResponse>?
  • If the response is of type Mono will the request be backpressured? Is there a need to convert it to bytes chunks and make it Flow?
  • Spring documentation says a lot of benefits of backpressure but doesn't answer the question above, nor gives any warning about Mono response. Is it still reactive?
  • Use `@RestController` not `@Controller`. – M. Deinum May 22 '18 at 10:04
  • Now Jackson kicks in and tries to serialize the response `nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.springframework.web.reactive.function.client.DefaultClientResponse` –  May 22 '18 at 11:23
  • 1
    @KrzysztofPalka I think error message is qutie clear - you have no serializer which can handle `ClientResponse`/`DefaultClientResponse` type. – splatch May 23 '18 at 09:27

0 Answers0