gpt4 book ai didi

java - bodyToMono ParameterizedTypeReference 和 bodyToFlux 有什么区别

转载 作者:行者123 更新时间:2023-12-01 16:43:43 48 4
gpt4 key购买 nike

我没有看到这两种方法之间有任何主要的时间复杂度差异,它们都有工作魅力,我想了解这两种方法之间的主要区别是什么

我正在从服务中获取 Student 对象的集合。

bodyToMono 参数化类型引用

public Mono<Collection<Student>> getStudents(String id) {

return webClient
.get()
.uri(uriBuilder -> uriBuilder
.path("/students/{0}")
.build(id))
.retrieve()
.onStatus(HttpStatus::isError, resp -> resp.createException()
.map(WebClientGraphqlException::new)
.flatMap(Mono::error)
).bodyToMono(new ParameterizedTypeReference<Collection<Student>>() {}); // This Line
}

bodyToFlux 收集器

public Mono<Collection<Student>> getStudents(String id) {

return webClient
.get()
.uri(uriBuilder -> uriBuilder
.path("/students/{0}")
.build(id))
.retrieve()
.onStatus(HttpStatus::isError, resp -> resp.createException()
.map(WebClientGraphqlException::new)
.flatMap(Mono::error)
).bodyToFlux(Student.class).collect(Collectors.toList()); // This Line
}

最佳答案

  • 如果您要检索单个项目,请使用 bodyToMono。它发出 0-1 个项目

  • 对于多个项目,请使用 bodyToFlux。它发出 0-N 个项目。

有关处理函数的更多信息 spring reactive web handler

关于java - bodyToMono ParameterizedTypeReference 和 bodyToFlux 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61814434/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com