Spring WebFlux 的新手,尝试在一个端点中返回字符串数组,由于某种原因,它返回一个连接的字符串而不是 JSON 数组。
用某个类包装它可以解决问题,但想知道如何实际返回字符串数组?返回例如 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-boot - 从 Spring WebFlux 返回 Flux<String> 返回一个字符串而不是 JSON 中的字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48421597/