我有一个REST控制器。它处理资源“消息”。每条消息都可以包含注释。
/api/v1/messages/1
/api/v1/messages/2
/api/v1/messages/1/comments/1
/api/v1/messages/1/comments/2
这是一段代码:
@RestController
@RequestMapping("/api/v1/messages/")
public class RestControllerV1 {
@RequestMapping(value = "{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<Message> getMessage(@PathVariable("id") Long messageId) {
}
@RequestMapping(value = "{messageId}/comments/{commentId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<Comment> getComment(@PathVariable("messageId") Long messageId, @PathVariable("commentId") Long commentId) {
}
}
工作正常。 但我认为为两种资源配备一个大型控制器并不是很好。所以我想要不同的控制器(SOLID,S-principle)。
MessageControllerV1
和
CommentControllerV1
是否可以在Spring Boot应用程序中划分控制器?
答案 0 :(得分:0)
你可以拥有类似的东西:
@RestController
@RequestMapping("/api/v1/messages")
public class MessageController {
@RequestMapping(value = "{messageId}",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<Message> getMessage(@PathVariable("messageId") Long messageId) {
...
}
}
@RestController
@RequestMapping("/api/v1/messages/{messageId}/comments")
public class CommentController {
@RequestMapping(value = "{commentId}",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<Comment> getComment(@PathVariable("messageId") Long messageId,
@PathVariable("commentId") Long commentId) {
...
}
}