现在在Spring 5中有两种暴露HTTP端点的方式。
- 通过使控制器类成为
@Controller
或@RestController
@Controller
@RestController
@RequestMapping("persons")
public class PersonController {
@Autowired
private PersonRepo repo;
@GetMapping("/{id}")
public Mono<Person> personById(@PathVariable String id){
retrun repo.findById(id);
}
}
- 通过使用RouterFunctions在
@Configuration
类中配置路由:
@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()));
}
在使用任何一种方法时,是否存在性能差异?在从头开始编写应用程序时,我应该使用哪种方法?