konrad-kaminski / spring-kotlin-coroutine

Kotlin coroutine support for Spring.
448 stars 69 forks source link

Suspendable controller methods returning ResponseEntity are not handled #14

Closed fabienmoritz closed 6 years ago

fabienmoritz commented 6 years ago

https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-ann-responseentity

The ResponseEntityResultHandler does not resolve the return type properly.

Example :


data class TestData(val value: String)

private val repo = mutableListOf<TestData>()

@RequestMapping("/testJson", produces = [MediaType.APPLICATION_JSON_UTF8_VALUE], consumes = [MediaType.APPLICATION_JSON_UTF8_VALUE])
@Controller
class JsonController {

    @GetMapping("/suspend/{value}")
    @ResponseBody
    suspend fun get(@PathVariable value: String) = repo.find { it.value == value }

// FAIL (IllegalStateException on ViewResolutionResultHandler because it is not handled by the ResponseEntityResultHandler)
    @PostMapping("/suspend")
    suspend fun createSuspend(@RequestBody value: TestData): ResponseEntity<TestData> {
        repo.add(value)
        delay(100)
        return ResponseEntity
                .created(URI("/testJson/suspend/${value.value}"))
                .body(value)
    }

// WORKS
    @PostMapping("/mono")
    fun create(@RequestBody value: TestData): Mono<ResponseEntity<TestData>> {
        repo.add(value)
        //delay(100)
        return Mono.just(ResponseEntity
                .created(URI("/testJson/suspend/${value.value}"))
                .body(value))
    }
}
konrad-kaminski commented 6 years ago

This an issue in Spring Core with incorrect return type of a suspending functions. I created a PR for solving it. Once it's solved it should work fine.

In the meantime I'll try to find some workaround.

konrad-kaminski commented 6 years ago

I'm closing this issue as this is a duplicate of #10. The workaround I mentioned can be found there.