在Hateoas链接上表示主体
如果该端点需要有效的@requestbody
,我有一个简单的问题,即如何通过Hateoas链接端点?我已经看过关于此的另一个问题这个问题还不够清楚。
查看以下代码:
@GetMapping(path = "/notification/{id}")
@ResponseStatus(HttpStatus.OK)
public NotificationItemResponse getNotification(@PathVariable final String id) {
return notificationItemMapper.toResponse(findUseCase.findNotification(id))
.add(linkTo(methodOn(NotificationItemController.class).getNotification(id)).withSelfRel())
.add(linkTo(methodOn(NotificationItemController.class).saveNotification()).withRel("save")) <- error here, saveNotification expects a valid request body
.add(linkTo(methodOn(NotificationItemController.class).revokeNotification(id)).withRel("revoke"))
.add(linkTo(methodOn(NotificationItemController.class).markNotificationAsSeen(id, "{userName}")).withRel("visualize"));
}
savenotification()
是该控制器上需要请求主体的方法。该请求主体是一个长长的JSON,其中包含通知消息,目标用户等。
创建一个虚拟主体只是为了传递在这里似乎是不正确的,并且传递无效的值似乎是不正确的。这里的正确方法是什么?如何正确链接需要请求主体的方法?更具体地说,我的要求是什么?
除了我以前作为非最佳解决方案所说的那样,这里的最佳做法是什么,除了通过无效的身体或虚拟机构?
I have a simple question regarding how do I link an endpoint through HATEOAS if that endpoint requires a valid @RequestBody
? I've seen another question regarding this but with no straight answers and so I wonder if the question was not clear enough.
Take a look at the code below:
@GetMapping(path = "/notification/{id}")
@ResponseStatus(HttpStatus.OK)
public NotificationItemResponse getNotification(@PathVariable final String id) {
return notificationItemMapper.toResponse(findUseCase.findNotification(id))
.add(linkTo(methodOn(NotificationItemController.class).getNotification(id)).withSelfRel())
.add(linkTo(methodOn(NotificationItemController.class).saveNotification()).withRel("save")) <- error here, saveNotification expects a valid request body
.add(linkTo(methodOn(NotificationItemController.class).revokeNotification(id)).withRel("revoke"))
.add(linkTo(methodOn(NotificationItemController.class).markNotificationAsSeen(id, "{userName}")).withRel("visualize"));
}
saveNotification()
is a method on this controller that requires a request body. That request body is a long json containing a notification message, target users, etc.
It doesn't seem right to create a dummy body just to pass down here, and it hardly seem right to pass a null value. What is the correct approach here? How do I correctly link a method that requires a request body? More specifically, what do I pass down as that request body?
What is the best practice here, other than passing a null or dummy body, as I stated before as a non-optimal solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题很旧,但是我今天遇到了同样的问题,很难找到正确的答案。
经过一些研究,我在春季Hateoas文档中找到了这个例子: 3.3负担
在这种情况下,他们使用一种方法负担(...),其工作原理与 linkto(...)非常相似。看起来通过无效物体是一种最佳实践,或者至少是春季团队鼓励它的。因此,在您的情况下,它看起来像这样:
The question is pretty old, but I faced the same issue today, and it looks pretty hard to find the correct answer.
After some research, I found this example in Spring HATEOAS Docs: 3.3 Affordances
In this case, they use a method afford(...), which works pretty similar to linkTo(...). Looks like passing a null object is a best practice, or at least it is encouraged by the Spring team. So in your case it would look like this: