()
先看一遍官方文档
通过注解的方式了解到,与 spirng mvc 不同之处在于Controller 方法的返回值(此外还有参数列表与方法体)不太一样。
@RestController@RequestMapping("/users")public class MyRestController { @GetMapping("/{user}") public MonogetUser(@PathVariable Long user) { // ... } @GetMapping("/{user}/customers") public Flux getUserCustomers(@PathVariable Long user) { // ... } @DeleteMapping("/{user}") public Mono deleteUser(@PathVariable Long user) { // ... }}
那么函数式呢?需要自己 编写路由 与 handler
@Configurationpublic class RoutingConfiguration { @Bean public RouterFunctionmonoRouterFunction(UserHandler userHandler) { return route(GET("/{user}").and(accept(APPLICATION_JSON)), userHandler::getUser) .andRoute(GET("/{user}/customers").and(accept(APPLICATION_JSON)), userHandler::getUserCustomers) .andRoute(DELETE("/{user}").and(accept(APPLICATION_JSON)), userHandler::deleteUser); }}@Componentpublic class UserHandler { public Mono getUser(ServerRequest request) { // ... } public Mono getUserCustomers(ServerRequest request) { // ... } public Mono deleteUser(ServerRequest request) { // ... }}
233
Flux 与 Mono