我可以理解这段代码找到了 BookController,因为我在 BookController 之前执行了 @ExposesResourceFor(Book.class)
但它如何知道要解析为哪个方法/URL?
entityLinks.linkForSingleResource(book).toUri();
解析为127.0.0.1:8080/books/5
它只是将/id 添加到 url 中并且根本不检查方法吗?
最佳答案
Spring HATEOAS 文档有 chapter on EntityLinks这显示了一个示例。
documentation on ControllerEntityLinks包含有关预期 URI 结构的更多详细信息。需要有一个针对集合的空映射,以及针对各个资源的带有 id 路径变量的映射。
@Controller
@ExposesResourceFor(Order.class)
@RequestMapping("/orders")
class OrderController {
//The collection resource
@RequestMapping
ResponseEntity orders(…) { … }
//Individual resources
@RequestMapping("/{id}")
ResponseEntity order(@PathVariable("id") … ) { … }
}
关于spring - linkForSingleResource 如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43736918/