从 Spring WebFlux returns 返回 Flux<String> 一个字符串而不是 JSON 中的字符串数组
Returning Flux<String> from Spring WebFlux returns one string instead of array of strings in JSON
Spring WebFlux 的新手,试图 return 一个端点中的字符串数组,由于某种原因,它 return 是一个连接的字符串而不是 JSON 数组。
用一些 class 包裹它解决了问题,但想知道如何实际 return 字符串数组?例如返回 Array<String>
按预期工作
class Wrapper(val data: String) {
@RestController
class Test() {
@RequestMapping("/wrapped") // Returns valid JSON array: [{"value":"Hello"},{"value":"World"}]
fun b() = Flux.just(Wrapper("Hello"),Wrapper("World"))
@RequestMapping("/raw") // Returns not valid JSON with just one concatenated string: HelloWorld
fun a() = Flux.just("Hello", "World")
}
在 Twitter https://twitter.com/sdeleuze/status/956136517348610048
中得到了 Sébastien Deleuze(Spring 框架提交者)的回答
Indeed when element type is String, the handler method is expected to provide directly well formed JSON String chunks, no serialization with Jackson is involved.
Spring WebFlux 的新手,试图 return 一个端点中的字符串数组,由于某种原因,它 return 是一个连接的字符串而不是 JSON 数组。
用一些 class 包裹它解决了问题,但想知道如何实际 return 字符串数组?例如返回 Array<String>
按预期工作
class Wrapper(val data: String) {
@RestController
class Test() {
@RequestMapping("/wrapped") // Returns valid JSON array: [{"value":"Hello"},{"value":"World"}]
fun b() = Flux.just(Wrapper("Hello"),Wrapper("World"))
@RequestMapping("/raw") // Returns not valid JSON with just one concatenated string: HelloWorld
fun a() = Flux.just("Hello", "World")
}
在 Twitter https://twitter.com/sdeleuze/status/956136517348610048
中得到了 Sébastien Deleuze(Spring 框架提交者)的回答Indeed when element type is String, the handler method is expected to provide directly well formed JSON String chunks, no serialization with Jackson is involved.