How to return 404 with Spring WebFlux -
i'm having controller 1 (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(...) } }
im wondering whether there better approach throwing exception annotated @responsestatus(code = not_found)
i lie use routefunction
instead of @restcontroller when spring 5 stable. define handlerfunction handle request, , declare routefunction
map request handlerfunction:
public mono<serverresponse> get(serverrequest req) { return this.posts .findbyid(req.pathvariable("id")) .flatmap((post) -> serverresponse.ok().body(mono.just(post), post.class)) .switchifempty(serverresponse.notfound().build()); }
kotlin version, , use routefunctiondsl
map incoming request handlerfuncation:
fun get(req: serverrequest): mono<serverresponse> { return this.posts.findbyid(req.pathvariable("id")) .flatmap { post -> ok().body(mono.just(post), post::class.java) } .switchifempty(notfound().build()) }
it can expression, like:
fun get(req: serverrequest): mono<serverresponse> = this.posts.findbyid(req.pathvariable("id")) .flatmap { post -> ok().body(mono.just(post), post::class.java) } .switchifempty(notfound().build())
Comments
Post a Comment