Spring 5 WebFlux中@Controller和RouterFunction之间的区别 [英] Difference between @Controller and RouterFunction in Spring 5 WebFlux

查看:1349
本文介绍了Spring 5 WebFlux中@Controller和RouterFunction之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在有两种方法可以在Spring 5公开HTTP端点.

There are two ways to expose HTTP endpoints in spring 5 now.

    通过设置控制器的类,例如
  1. @Controller@RestController
  1. @Controller or @RestController by making the controller's class, e.g.

@RestController
@RequestMapping("persons")
public class PersonController { 

    @Autowired
    private PersonRepo repo;

    @GetMapping("/{id}")
    public Mono<Person> personById(@PathVariable String id){
        retrun repo.findById(id);
    }
}

  1. 使用RouterFunction在@Configuration类中进行路由:
  1. Route in @Configuration class by using RouterFunctions:

@Bean
public RouterFunction<ServerResponse> personRoute(PersonRepo repo) {
    return route(GET("/persons/{id}"), req -> Mono.justOrEmpty(req.pathVariable("id"))                                             
                                                 .flatMap(repo::getById)
                                                 .flatMap(p -> ok().syncBody(p))
                                                 .switchIfEmpty(notFound().build()));
}

使用任何人的方法是否有性能差异?从头开始编写应用程序时应该使用哪一个.

Is there any performance difference in using anyone approach? Which one should I use when writing my application from scratch.

推荐答案

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆