如何在 Spring Webflux 控制器中结合 Flux 和 ResponseEntity
How to combine Flux and ResponseEntity in Spring Webflux controllers
我在我的 Webflux 控制器中使用 Mono
s 和 ResponseEntity
s 来操纵 headers 和其他响应信息。例如:
@GetMapping("/{userId}")
fun getOneUser(@PathVariable userId: UserId): Mono<ResponseEntity<UserDto>> {
return repository.findById(userId)
.map(User::asDto)
.map { ResponseEntity.ok(it) }
.defaultIfEmpty(ResponseEntity.notFound().build())
}
@GetMapping
fun getAllUsers(): Flux<UserDto> {
return repository.findAllActive().map(User::asDto)
}
两者都可以正常工作,但在某些情况下也需要 ResponseEntity
与 Flux
结合使用。响应类型应该是什么?使用 ResponseEntity<Flux<T>>
是否正确?
例如:
@GetMapping("/{userId}/options")
fun getAllUserOptions(@PathVariable userId: UserId): ??? {
return repository.findById(userId)
.flatMapIterable{ it.options }
.map { OptionDto.from(it) }
// if findById -> empty Mono then:
// return ResponseEntity.notFound().build() ?
// else:
// return the result of `.map { OptionDto.from(it) }` ?
}
我想在这里实现的行为是 getAllUserOptions returns 404
如果 repository.findById(userId)
为空 Mono
,否则 return user.options
作为 Flux
.
更新:
这里的存储库是 ReactiveCrudRepository
使用switchIfEmpty
在用户不存在的情况下抛出异常:
return repository
.findById(userId)
.switchIfEmpty(Mono.error(NotFoundException("User not found")))
.flatMapIterable{ it.options }
.map { OptionDto.from(it) }
然后使用 exception handler 将其转换为 404 响应。
您可以通过 ResponseEntity 返回 Mono 来使用
像这样
public Mono<ResponseEntity<?>> oneRawImage(
@PathVariable String filename) {
// tag::try-catch[]
return imageService.findOneImage(filename)
.map(resource -> {
try {
return ResponseEntity.ok()
.contentLength(resource.contentLength())
.body(new InputStreamResource(
resource.getInputStream()));
} catch (IOException e) {
return ResponseEntity.badRequest()
.body("Couldn't find " + filename +
" => " + e.getMessage());
}
});
}
我也有这样的例子
public ResponseEntity<Mono<?>> newLive(@Valid @RequestBody Life life) {
Mono<Life> savedLive = liveRepository.save(life);
if (savedLive != null) {
return new ResponseEntity<>(savedLive, HttpStatus.CREATED);
}
return new ResponseEntity<>(Mono.just(new Life()), HttpStatus.I_AM_A_TEAPOT);
}
我不喜欢 REST 控制器中的函数式编程。
这是一个例子ReactiveController。
适合我,如果有问题请告诉我
@PostMapping(value = "/bizagi/sendmsg")
public Mono<ResponseEntity<?>> sendMessageToQueue(@RequestBody BizagiPost bizagiPost) {
Mono<BodyReturn> retorno = useCase.saveMsg(bizagiPost);
Map<String, Object> response = new HashMap<>();
return retorno.map(t ->
{
if (t.getStatusCode().equals("200")) {
response.put("message", t.getReazon());
return new ResponseEntity(t, HttpStatus.OK);
} else {
response.put("message", t.getReazon());
return new ResponseEntity(t, HttpStatus.BAD_REQUEST);
}
});
}
我在我的 Webflux 控制器中使用 Mono
s 和 ResponseEntity
s 来操纵 headers 和其他响应信息。例如:
@GetMapping("/{userId}")
fun getOneUser(@PathVariable userId: UserId): Mono<ResponseEntity<UserDto>> {
return repository.findById(userId)
.map(User::asDto)
.map { ResponseEntity.ok(it) }
.defaultIfEmpty(ResponseEntity.notFound().build())
}
@GetMapping
fun getAllUsers(): Flux<UserDto> {
return repository.findAllActive().map(User::asDto)
}
两者都可以正常工作,但在某些情况下也需要 ResponseEntity
与 Flux
结合使用。响应类型应该是什么?使用 ResponseEntity<Flux<T>>
是否正确?
例如:
@GetMapping("/{userId}/options")
fun getAllUserOptions(@PathVariable userId: UserId): ??? {
return repository.findById(userId)
.flatMapIterable{ it.options }
.map { OptionDto.from(it) }
// if findById -> empty Mono then:
// return ResponseEntity.notFound().build() ?
// else:
// return the result of `.map { OptionDto.from(it) }` ?
}
我想在这里实现的行为是 getAllUserOptions returns 404
如果 repository.findById(userId)
为空 Mono
,否则 return user.options
作为 Flux
.
更新:
这里的存储库是 ReactiveCrudRepository
使用switchIfEmpty
在用户不存在的情况下抛出异常:
return repository
.findById(userId)
.switchIfEmpty(Mono.error(NotFoundException("User not found")))
.flatMapIterable{ it.options }
.map { OptionDto.from(it) }
然后使用 exception handler 将其转换为 404 响应。
您可以通过 ResponseEntity 返回 Mono 来使用
像这样
public Mono<ResponseEntity<?>> oneRawImage(
@PathVariable String filename) {
// tag::try-catch[]
return imageService.findOneImage(filename)
.map(resource -> {
try {
return ResponseEntity.ok()
.contentLength(resource.contentLength())
.body(new InputStreamResource(
resource.getInputStream()));
} catch (IOException e) {
return ResponseEntity.badRequest()
.body("Couldn't find " + filename +
" => " + e.getMessage());
}
});
}
我也有这样的例子
public ResponseEntity<Mono<?>> newLive(@Valid @RequestBody Life life) {
Mono<Life> savedLive = liveRepository.save(life);
if (savedLive != null) {
return new ResponseEntity<>(savedLive, HttpStatus.CREATED);
}
return new ResponseEntity<>(Mono.just(new Life()), HttpStatus.I_AM_A_TEAPOT);
}
我不喜欢 REST 控制器中的函数式编程。
这是一个例子ReactiveController。
适合我,如果有问题请告诉我
@PostMapping(value = "/bizagi/sendmsg")
public Mono<ResponseEntity<?>> sendMessageToQueue(@RequestBody BizagiPost bizagiPost) {
Mono<BodyReturn> retorno = useCase.saveMsg(bizagiPost);
Map<String, Object> response = new HashMap<>();
return retorno.map(t ->
{
if (t.getStatusCode().equals("200")) {
response.put("message", t.getReazon());
return new ResponseEntity(t, HttpStatus.OK);
} else {
response.put("message", t.getReazon());
return new ResponseEntity(t, HttpStatus.BAD_REQUEST);
}
});
}