首页 > 解决方案 > 如何使用 Kotlin 操作 Mono 对象

问题描述

大家好,我正在使用 webflux 和 kotlin 开发一个 API。我的问题很简单,我需要在使用 API 时操作返回给我的 Flux 对象的属性,这是伤害我的对象

postId": 14,
    "userId": 2,
    "title": "voluptatem eligendi optio",
    "comments": [
        {
            "email": "Janice@alda.io",
            "body": "necessitatibus libero occaecati\nvero inventore iste assumenda veritatis\nasperiores non sit et quia omnis facere nemo explicabo\nodit quo nobis porro"
        }
 ]

这是我的控制器,它发出请求并返回之前的 JSON 对象

fun getData(): Flux<Post> {

    return apiService.fetchPosts()
}

这是我使用 API 的服务代码

@Service
class APIService {
fun fetchComments(postId:Int) =
        fetch("posts/$postId/comments").bodyToFlux(Comment::class.java)

fun fetchPosts() =
        fetch("/posts").bodyToFlux(Post::class.java)

fun fetch(path:String): WebClient.ResponseSpec {
    val client = WebClient.create("http://jsonplaceholder.typicode.com/")
    return client.get().uri(path).retrieve()
 }
}

谢谢

标签: jsonobjectkotlinspring-webfluxflux

解决方案


推荐阅读