Closed
Description
Affects: 5.3.1
Bug
I'm using the kotlinx-serialization library. The library is properly set up, jackson is excluded from dependencies, and returning a class annotated with @Serializable
works correctly.
I'm trying to return a simple Kotlin list from a controller. When accessing the route the server displays this error and nothing is returned:
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
@RestController
class Controller {
@GetMapping("/")
fun notworking() = listOf(1, 2, 3)
}
Error message
2020-11-24 17:28:51.929 WARN 189362 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.Arrays$ArrayList]
2020-11-24 17:28:51.945 WARN 189362 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.LinkedHashMap]
What I expected
I expected the list to be serialized properly like this:
[1,2,3]
Workarounds
Those three workarounds work:
@Serializable
data class Wrapper(val content: List<Int>)
@GetMapping("/")
fun working() = Wrapper(listOf(1, 2, 3))
@GetMapping("/", produces = ["application/json"])
fun working() = Json.encodeToString(listOf(1, 2, 3))
@GetMapping("/working")
fun working() = Json.encodeToJsonElement(listOf(1, 2, 3))