首先,我知道还有几个问题本质上是在问同样的问题(How to Wrap Flux in a ResponseEntity,How to combine Flux and ResponseEntity in Spring Webflux controllers),但最终答案最终都返回了Mono<ResponseEntity>
。
ResponseEntity<Mono<T>>
给定CustomersService
中的一个CustomersService
方法,我的控制器代码如下所示:
@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>>
,那么控制器代码应该是什么样的呢?
public Flux<ResponseEntity<Customer> getCustomers(String name) {
return customersService
.getCustomers(name)
...?
}
发布于 2022-09-27 15:22:14
和你的第一个案子一样。
@Autowired
CustomersService customersService;
public Flux<ResponseEntity<Customer>> getCustomers(String name) {
return customersService.getCustomers(name)
.map(ResponseEntity::ok)
.defaultIfEmpty(ResponseEntity.noContent().build());
}
https://stackoverflow.com/questions/73867994
复制相似问题