id パラメータを受け取り、delete product api を送信して削除するエンドポイントがあります。productService.delete も Mono を返します。問題は、productService.delete メソッドが mono エラーを返し、エンドポイントが常に http 200 で応答することです。この mono エラーに関するエラー ログを確認できますが、ハンドラー メソッドは http 200 を応答します。
APIで例外を処理するAbstractErrorWebExceptionHandlerがあります。しかし、Mono のため、エラー ハンドラはこの問題を処理できません。ダウンストリームで例外が発生した場合、Spring webflux はこのエラーを認識し、http 200 で応答しません。
public Mono<ServerResponse> deleteProduct(ServerRequest request) {
String id = request.pathVariable("id");
Mono<Product> productMono = this.repository.findById(id);
return productMono
.flatMap(existingProduct ->
ServerResponse.noContent()
.build(productService.delete(existingProduct))
);
}
ソースコードでは、指定された発行者が完了すると応答がコミットされると書かれています。しかし、 error complete はどうですか? Spring webflux はエラー信号かどうかをチェックしていないと思います。モノコンプリートかどうかを確認してください。
* Build the response entity with no body.
* The response will be committed when the given {@code voidPublisher} completes.
* @param voidPublisher publisher publisher to indicate when the response should be committed
* @return the built response
*/
Mono<ServerResponse> build(Publisher<Void> voidPublisher);
前もって感謝します。