本文介绍了如何使用Spring WebFlux返回404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像这样的控制器(在Kotlin中):
I'm having a controller like this one (in Kotlin):
@RestController
@RequestMapping("/")
class CustomerController (private val service: CustomerService) {
@GetMapping("/{id}")
fun findById(@PathVariable id: String,
@RequestHeader(value = IF_NONE_MATCH) versionHeader: String?): Mono<HttpEntity<KundeResource>> =
return service.findById(id)
.switchIfEmpty(Mono.error(NotFoundException()))
.map {
// ETag stuff ...
ok().eTag("...").body(...)
}
}
我想知道是否有比抛出带有@ResponseStatus(code = NOT_FOUND)
Im wondering whether there is a better approach than throwing an exception which is annotated with @ResponseStatus(code = NOT_FOUND)
推荐答案
可以将方法的实现更改为
Instead of throwing an exception the method's implementation can be changed to
fun findById(@PathVariable id: String,
@RequestHeader(value = IF_NONE_MATCH) versionHeader: String?): Mono<ResponseEntity<KundeResource>> =
return service.findById(id)
.map {
// ETag stuff ...
ok().eTag("...").body(...)
}
.defaultIfEmpty(notFound().build())
这篇关于如何使用Spring WebFlux返回404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!