【发布时间】:2018-10-29 15:38:05
【问题描述】:
我在 Rest Web 服务中使用 RxJava2 Observable.fromIterable()。 我的可迭代示例由三个元素组成,但我的非阻塞休息服务仅返回三个元素中的一个。
class ToDoDaoImpl implements ToDoDao {
Map<String, ToDo> toDos;
...
public Observable<ToDo> readAll() {
return Observable.fromIterable(toDos.entrySet().stream().map(entry -> entry.getValue()).collect(Collectors.toList()));
}
}
当我从我的 Non-Blocking Rest 库中调用 readAll() 方法时,我只能在三个上得到一个元素:
@Api(path = "/api/v2/read", method = "GET", produces = "application/json")
Action readAllToDos = (HttpServletRequest request, HttpServletResponse response) -> {
Observable.just(request)
.flatMap(req -> toDoDao.readAll())
.subscribe(output -> toJsonResponse(request, response, new ResponseDto(200, output)),
error -> toJsonResponse(request, response, new ResponseDto(200, error))
);
};
我的输出:
{
"status": 200,
"response": {
"id": "5dc74dd8-1ea9-427e-8bb7-482cc6e24c71",
"title": "learn ReactiveJ",
"description": "learn to use ReactiveJ library",
"date": {
"year": 2018,
"month": 10,
"day": 29
}
},
"datetime": "Oct 29, 2018 4:19:51 PM"
}
如果我调用我的 Dao 的 Non-reactive 等价物,我会得到我所期望的:
{
"status": 200,
"response": [
{
"id": "25cbe3bf-12be-42e4-82ce-d4780f6469f6",
"title": "study reactive",
"description": "learn reactive programming",
"date": {
"year": 2018,
"month": 10,
"day": 29
}
},
{
"id": "51879241-f005-43fa-80fb-78386b663cb7",
"title": "learn ReactiveJ",
"description": "learn to use ReactiveJ library",
"date": {
"year": 2018,
"month": 10,
"day": 29
}
},
{
"id": "80a07c1b-2317-4eb8-9a39-ac35260f37a2",
"title": "exercise",
"description": "do some exercises",
"date": {
"year": 2018,
"month": 10,
"day": 29
}
}
],
"datetime": "Oct 29, 2018 4:37:05 PM"
}
【问题讨论】:
标签: java observable reactive-programming rx-java2