Difference between @Controller and RouterFunction in Spring 5 WebFlux
There are two ways to expose HTTP endpoints in spring 5 now.
@Controlleror@RestControllerby 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);
}
}
- Route in
@Configurationclass 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.