【发布时间】:2018-11-14 00:30:26
【问题描述】:
我在 Spring Boot 项目中使用 Spring Webflux 和 ReactiveMongoRepository,我希望每个通量都是一个对象列表,例如:
// 1st Flux Started:
[
{
// 1st Dashboard
},
{
// 2nd Dashboard
}
]
// 1st Flux Ended
// 2nd Flux Started:
[
{
// 1st Dashboard
},
{
// 2nd Dashboard
}
]
// 2nd Flux Ended
存储库:
@Repository
public interface ReactiveDashboardRepository extends ReactiveMongoRepository<Dashboard, String> {
}
服务:
@Service
public class ReactiveDashboardServiceImpl implements ReactiveDashboardService {
private ReactiveDashboardRepository reactiveDashboardRepository;
public ReactiveDashboardServiceImpl(ReactiveDashboardRepository reactiveDashboardRepository) {
this.reactiveDashboardRepository = reactiveDashboardRepository;
}
public Flux<Dashboard> getDashboards() {
return this.reactiveDashboardRepository.findAll();
}
}
控制器:
@CrossOrigin
@RestController
@RequestMapping("/api/sse")
public class ReactiveDashboardRestController {
private ReactiveDashboardService reactiveDashboardService;
public ReactiveDashboardRestController(ReactiveDashboardService reactiveDashboardService) {
this.reactiveDashboardService = reactiveDashboardService;
}
@GetMapping(value = "/dashboards", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<Dashboard> getDashboards() {
return this.reactiveDashboardService.getDashboards();
}
}
所以基本上我希望数组中的所有对象都成为单个流的一部分,因为这就是我将使用的客户端应用程序的设计方式。目前总共只有3个。所以在每个流中都会保存一个所有对象的数组。我知道这不是 webflux 的最佳用途。 如果可以实现,如何实现?
【问题讨论】:
-
您要将哪些数据转换为该表单?更具体一些,并给出一些代码。
-
我通过添加额外信息来编辑我的问题。
-
它们在 Dashboard 类中是否有任何属性,可以使用哪些属性对通量进行分组?如果是,那么您可以将类似的通量组合在一起,最终获得仪表板列表的通量。
标签: spring-webflux