当Spring 5稳定时,我想使用 RouteFunction 而不是@RestController . 定义HandlerFunction以处理请求,然后声明 RouteFunction 以将请求映射到HandlerFunction:
public Mono get(ServerRequest req) {
return this.posts
.findById(req.pathVariable("id"))
.flatMap((post) -> ServerResponse.ok().body(Mono.just(post), Post.class))
.switchIfEmpty(ServerResponse.notFound().build());
}
检查完整的示例代码here .
Kotlin版本,定义一个处理请求的函数,使用 RouteFunctionDSL 将传入的请求映射到HandlerFuncation:
fun get(req: ServerRequest): Mono {
return this.posts.findById(req.pathVariable("id"))
.flatMap { post -> ok().body(Mono.just(post), Post::class.java) }
.switchIfEmpty(notFound().build())
}
它可以是一个表达式,如:
fun get(req: ServerRequest): Mono = this.posts.findById(req.pathVariable("id"))
.flatMap { post -> ok().body(Mono.just(post), Post::class.java) }
.switchIfEmpty(notFound().build())
查看Kotlin DSL here的完整示例代码 .
如果您更喜欢传统控制器来公开REST API,请尝试这种方法 .
首先定义一个例外,例如 . PostNotFoundException . 然后把它扔进控制器 .
@GetMapping(value = "/{id}") public Mono get(@PathVariable(value = "id") Long id) { return this.posts.findById(id).switchIfEmpty(Mono.error(new PostNotFoundException(id))); }
定义 ExceptionHandler 来处理异常,并在 HttpHandler 中注册它 .
@Profile("default")
@Bean
public NettyContext nettyContext(ApplicationContext context) {
HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context)
.exceptionHandler(exceptionHandler())
.build();
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create("localhost", this.port);
return httpServer.newHandler(adapter).block();
}
@Bean
public WebExceptionHandler exceptionHandler() {
return (ServerWebExchange exchange, Throwable ex) -> {
if (ex instanceof PostNotFoundException) {
exchange.getResponse().setStatusCode(HttpStatus.NOT_FOUND);
return exchange.getResponse().setComplete();
}
return Mono.error(ex);
};
}
在这里查看complete codes . 对于Spring Boot用户,请检查this sample .