我在MVC控制器中具有以下get端点:
@GetMapping("/xx/xxxxxxx/xxxxx/{name}")
public Flux<ResponseEntity<Event>> getRecords(@PathVariable("name") String name) {
return eventService.getRecordsByName(name)
.map(record -> new ResponseEntity<>(record, HttpStatus.OK))
.switchIfEmpty(Mono.just(new ResponseEntity<>(HttpStatus.NO_CONTENT)))
.onErrorResume(error -> {
LOGGER.error("Error occurred in controller", error);
throw Exceptions.propagate(error);
});
}
被调用的服务方法如下:
@Override
public Flux<Record> getRecordsByName(String name) {
Optional<List<Record>> recordsOptional = repository.findAllByName(name);
if (recordsOptional.isPresent()) {
Flux<Record> recordFlux = Flux.fromIterable(recordsOptional.get());
return recordFlux;
}
return Flux.empty();
}
存储库如下:
@Repository("recordRepository")
@Transactional
public interface RecordRepository<T extends MyDocument, U extends String>
extends JpaRepository<Event, String> {
@Query(name = "query.records.byName", nativeQuery = true)
Optional<List<Record>> findAllByName(@Param("name") String name);
}
每当我调用get端点时,都会得到以下响应:
{
"timestamp": "2019-07-17T18:54:02.816+0000",
"path": "/xx/xxxxxxx/xxxxx/SomebodysNameHere",
"status": 500,
"error": "Internal Server Error",
"message": "Only a single ResponseEntity supported"
}
存储库的设置方式是否存在根本性的错误?我感觉好像在这里丢失了一些东西,但是我必须能够从控制器返回一个Flux
。如果我使用Mono<List>
可以正常工作,但这是不可接受的。