在Hateoas链接上表示主体

发布于 2025-01-21 23:33:01 字数 1219 浏览 0 评论 0原文

如果该端点需要有效的@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅沫记忆 2025-01-28 23:33:01

这个问题很旧,但是我今天遇到了同样的问题,很难找到正确的答案。

经过一些研究,我在春季Hateoas文档中找到了这个例子: 3.3负担

@GetMapping("/employees/{id}")
public EntityModel<Employee> findOne(@PathVariable Integer id) {

  Class<EmployeeController> controllerClass = EmployeeController.class;

  // Start the affordance with the "self" link, i.e. this method.
  Link findOneLink = linkTo(methodOn(controllerClass).findOne(id)).withSelfRel(); 

  // Return the affordance + a link back to the entire collection resource.
  return EntityModel.of(EMPLOYEES.get(id), //
      findOneLink //
          .andAffordance(afford(methodOn(controllerClass).updateEmployee(null, id))) 
          .andAffordance(afford(methodOn(controllerClass).partiallyUpdateEmployee(null, id)))); 
}

在这种情况下,他们使用一种方法负担(...),其工作原理与 linkto(...)非常相似。看起来通过无效物体是一种最​​佳实践,或者至少是春季团队鼓励它的。因此,在您的情况下,它看起来像这样:

.add(linkTo(methodOn(NotificationItemController.class).saveNotification(null)).withRel("save"))

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

@GetMapping("/employees/{id}")
public EntityModel<Employee> findOne(@PathVariable Integer id) {

  Class<EmployeeController> controllerClass = EmployeeController.class;

  // Start the affordance with the "self" link, i.e. this method.
  Link findOneLink = linkTo(methodOn(controllerClass).findOne(id)).withSelfRel(); 

  // Return the affordance + a link back to the entire collection resource.
  return EntityModel.of(EMPLOYEES.get(id), //
      findOneLink //
          .andAffordance(afford(methodOn(controllerClass).updateEmployee(null, id))) 
          .andAffordance(afford(methodOn(controllerClass).partiallyUpdateEmployee(null, id)))); 
}

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:

.add(linkTo(methodOn(NotificationItemController.class).saveNotification(null)).withRel("save"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文