使用 WebFlux 和 Spring 引导返回 kotlin 数据 class

Returning kotlin data class using WebFlux and Spring boot

我正在尝试 return 使用数据 class 的 ServerResponse,但出现此错误。

Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class org.springframework.web.reactive.function.server.DefaultEntityResponseBuilder$DefaultEntityResponse

我认为这是因为它没有找到 public getter。

我回复的顶部:

data class MyDataResponse(
        @SerializedName("metadata") val metadata:Metadata,
        @SerializedName("mydata") val my data: MyData?)

如何使用:

fun getMyData(): Mono<ServerResponse> = ServerResponse
        . ok().body(Mono.just(myService.getMyData()), MyDataResponse::class.java)

那么,我可以在 Kotlin 中执行此操作吗,还是我需要在 Java 中编写我的模型?

我找到了一个解决方案,它并不理想,但确实有效。

fun getMyData(): MyDataResponse { 
return myService.getMyData()
}

这个returns一个JSON对象,在我的例子中。

我使用了 data class 并且有效。

据我所知,您正在使用 Gson 反序列化您的数据。 WebFlux 中默认的序列化器是 Jackson,它支持 Kotlin(反)序列化。

here, you need to add the Jackson Kotlin Module 所述,作为依赖项,将自动拾取并注册。