问题描述
我对 REST API 开发很陌生.我决定使用 Spring Boot 创建一个博客应用程序,但我真的在为我的应用程序的设计和结构而苦苦挣扎.
I'm quite new into REST API development. I have decided to create a Blog application using Spring Boot and I'm really struggling with the design and structure of my app.
现在我的应用由 Post 和 Comment 模型和存储库组成.对于这两种模型,我都创建了服务类(PostService 和 CommentService).在这些类中,我拥有所有业务逻辑(现在只是简单的 CRUD).
Right now my app consists of Post and Comment models and repositories. For both models, I have created service classes (PostService and CommentService). In these classes, I have all the business logic (just simple CRUD right now).
现在我正在为我的帖子@RestControler 的设计而挠头.在 PostController 中,我公开了这些操作:
Now I am scratching my head about the design of my @RestControler for Posts. In PostController I have exposed these actions:
@PostMapping("/api/posts/create")
public Post create(@RequestBody Post post) { ... }
@GetMapping("/api/posts")
public List<Post> findAll() { ... }
@GetMapping("/api/posts/{id}")
public Post findById(@PathVariable("id") Long id) { ... }
@PutMapping("/api/posts/{id}")
public Post update(@RequestBody Post post) { ... }
@DeleteMapping("/api/posts/{id}")
public void delete(@PathVariable Long id) { ... }
现在我要回答我的问题了.我想知道在帖子中添加评论的正确设计是什么.
Now I'm getting to my question. I am wondering what is correct design of adding a Comment to the Post.
- 是否应该使用 CommentController 类公开所有用于 Comment 的 CRUD 方法并使用 create 方法?
- 是否可以向 PostController 添加新方法
addComment
以创建新评论?
- Should I expose all CRUD method for Comment using CommentController class and use create method?
- Is it ok to add a new method
addComment
to PostController which will create a new Comment?
在我的脑海里给帖子添加评论属于帖子,但我真的不知道.
In my head adding a Comment to the Post belongs to the Post, but I really don't know.
有人可以就这件事给我一些建议吗?
Could some of give me some advice regarding this matter?
非常感谢!
再见,汤姆
推荐答案
如果我是你,我会考虑 OpenAPI 规范 并将遵循 resource ->子资源 ->方法||标识符代码> 模式.出于可读性和理解目的,这可能是最亲吻和最干净的设计.
If I were you, I'd consider REST Design Principles from the OpenAPI Specification and would follow resource -> sub-resource -> method||identifier
pattern. This would probably be the most KISS and clean design for the readability and understanding purposes.
@PostMapping("/api/posts/") //you don't need /create as a separate URI
public Post create(@RequestBody Post post) { ... }
@GetMapping("/api/posts") //This is OK.
public List<Post> findAll() { ... }
@GetMapping("/api/posts/{id}") //OK, however {id} should be optional, hence you can combine this and upper methods in one method.
public Post findById(@PathVariable("id") Long id) { ... }
@PutMapping("/api/posts/{id}") //OK.
public Post update(@RequestBody Post post) { ... }
@DeleteMapping("/api/posts/{id}") //OK.
public void delete(@PathVariable Long id) { ... }
现在,对于评论 API 设计,我会将它们包含在 posts 资源下,并添加这些相应的 URI:
and now, for the comments API design, I would have contain them under posts resource, and would have added these corresponding URIs:
@GetMapping("/api/posts/{id}/comments/{commendId}") //commentId is optional
@PostMapping("/api/posts/{id}/comments/") //you don't need any {commendId} here, just post the payload
等等.我希望你能想出方法签名和其他方法映射.
and etc. I hope you can come up with method signatures and other method mappings.
您还可以在此处
这篇关于RestController 设计之争 - Spring Boot REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!