spring-boot - 从 Spring WebFlux 返回 Flux<String> 返回一个字符串而不是 JSON 中的字符串数组

标签 spring-boot kotlin spring-webflux

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/

相关文章:

java - BottomSheet 在 styles.xml 中声明的 "android:background"上不透明

java - 在 Kotlin 中使用 Java Void 类型

apache-kafka - 如何在 Webflux 应用程序中制作 Spring Cloud Stream 消费者?

spring-boot - 如何在 spring-webflux RouterFunction 端点中使用 OpenApi 注释?

postgresql - Postgres DDL 错误 : 'syntax error at or near "user"'

java - 在 ec2 中部署类似服务的 springboot 问题

android - PrintManager.print() 并更改了 attachBaseContext 中的语言环境

java - 如何使用 java sdk 和 web-flux 将 multipart/form-data 上传到 AmazonS3?

java - 以通用方式管理任何 HTTP 请求

java - 我们可以将下面的类自动连接到springboot中的配置类中吗