error-handling - Spring Webflux:根据抛出的异常返回状态码和消息
问题描述
如何在 Spring Webflux 中确定抛出哪个异常并从中获取状态码。这是我的控制器代码的结构。
@GetMapping("/")
fun getResults() : Mono<ResponseEntity<AccountDTO>>{
return Service.getResult()
.map {
}.doOnError {
//how to get statuscode here
throw ResponseStatusException(HttpStatus.NOT_FOUND, it.message!!)
}
在这里我可以得到抛出的自定义消息,但是如何获取状态码呢?而不是 HttpStatus.NOT_FOUND。我想捕获服务层抛出的状态码。或者有没有办法抛出异常?
解决方案
I found a solution that works.
@GetMapping("/")
fun getResults() : Mono<ResponseEntity<AccountDTO>>{
return Service.getResult()
.map {
}.doOnError {
if(it is NotFoundException)
{
throw ResponseStatusException(HttpStatus.NOT_FOUND)
}
else{
throw ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR)
}
}
推荐阅读
- c# - 在哪个 .Net Framework 版本 C# 7.2 可用
- javascript - Object.create() 使用
- python - 在 python 中启动键绑定
- ios - 未发送推送通知 quickblox
- java - CompletableFuture runAsync 与新线程
- solr - 使用 Solr 索引共享文件夹
- javascript - 单击正文而不是两个 div
- sql-server-2008 - 结合 3 个 sql 查询的结果来计算运行库存
- zend-framework3 - zend 框架 3 - 获取 sql 字符串
- entity-framework - 实体框架 - 跨链接表查询所有记录