我有一个像这样的控制器(在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)
@ResponseStatus(code = NOT_FOUND)
可以将方法的实现更改为:
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())