【发布时间】:2021-04-13 15:22:06
【问题描述】:
我有以下代码:
@GetMapping("/enrollment/{id}")
public Mono<ResponseEntity<Response>> findByEnrollment(@PathVariable("id") String enrollment){
return Mono.just(enrollment)
.flatMap(e -> technomechanicalService.findByEnrollmentVehicle(e)
.collectList()
.map(t -> ResponseEntity.ok()
.body(new Response(t, HttpStatus.OK.value(), null)))
.defaultIfEmpty(new ResponseEntity<>(new Response(null, HttpStatus.BAD_REQUEST.value(), "No se encontro la placa!"),
HttpStatus.BAD_REQUEST)));
}
当使用 mongodb 中不存在的值从客户端(邮递员)消费时,它不会转到 defaultIfEmpty。
我给你的解决方案是在地图前加一个过滤器。如下图:
@GetMapping("/enrollment/{id}")
public Mono<ResponseEntity<Response>> findByEnrollment(@PathVariable("id") String enrollment){
return Mono.just(enrollment)
.flatMap(e -> technomechanicalService.findByEnrollmentVehicle(e)
.collectList()
.filter(t -> {
if(!t.isEmpty()) return true;
return false;
})
.map(t -> ResponseEntity.ok()
.body(new Response(t, HttpStatus.OK.value(), null)))
.defaultIfEmpty(new ResponseEntity<>(new Response(null, HttpStatus.BAD_REQUEST.value(), null),
HttpStatus.BAD_REQUEST)));
}
我的问题是 .defaultIfEmpty () 会去哪里?
【问题讨论】:
-
it does not go to the defaultIfEmpty请更新它的去向?
标签: java spring-boot spring-webflux project-reactor