Difference between @Controller and RouterFunction in Spring 5 WebFlux
03:46 03 Nov 2017

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

  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 personById(@PathVariable String id){
        retrun repo.findById(id);
    }
}
  1. Route in @Configuration class by using RouterFunctions:
@Bean
public RouterFunction 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.

spring spring-boot spring-webflux reactive