首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Flux<T>中封装ResponseEntity (ResponseEntity<Flux<T>)

如何在Flux<T>中封装ResponseEntity (ResponseEntity<Flux<T>)
EN

Stack Overflow用户
提问于 2022-09-27 12:57:34
回答 1查看 47关注 0票数 0

首先,我知道还有几个问题本质上是在问同样的问题(How to Wrap Flux in a ResponseEntityHow to combine Flux and ResponseEntity in Spring Webflux controllers),但最终答案最终都返回了Mono<ResponseEntity>

ResponseEntity<Mono<T>>

给定CustomersService中的一个CustomersService方法,我的控制器代码如下所示:

代码语言:javascript
运行
复制
@Autowired CustomersService customersService;

public Mono<ResponseEntity<Customer> getCustomer(Long customerId) {
  return customersService
    .getCustomer(customerId)
    .map(ResponseEntity::ok)
    .defaultIfEmpty(ResponseEntity.notFound().build())
}

ResponseEntity<Flux<T>>

现在,如果将服务改为Flux<Customer> getCustomers(String name),并且控制器返回类型为ResponseEntity<Flux<Customer>>,那么控制器代码应该是什么样的呢?

代码语言:javascript
运行
复制
public Flux<ResponseEntity<Customer> getCustomers(String name) {
  return customersService
    .getCustomers(name)
    ...?
}
EN

回答 1

Stack Overflow用户

发布于 2022-09-27 15:22:14

和你的第一个案子一样。

代码语言:javascript
运行
复制
@Autowired 
CustomersService customersService;

public Flux<ResponseEntity<Customer>> getCustomers(String name) {
    return customersService.getCustomers(name)
            .map(ResponseEntity::ok)
            .defaultIfEmpty(ResponseEntity.noContent().build());
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73867994

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档