简体   繁体   English

将Spring数据休息链接与同一实体上的控制器链接合并

[英]Merge Spring data rest links with controller links on same Entity

I would like to combine HATEAOS links to methods on both Controller and Repository. 我想将HATEAOS链接与Controller和Repository上的方法结合起来。

@RepositoryRestController
@ResponseBody
@ExposesResourceFor(Group.class)
@RequestMapping(value = "/api/v2/groups", produces = MediaTypes.HAL_JSON_VALUE)
public class GroupController {

    @Resource
    private GroupService groupService;

    @RequestMapping(value = "/external", method = POST)
    public  @ResponseBody   PersistentEntityResource saveExternalGroup(
            @RequestBody Group newGroup,
            PersistentEntityResourceAssembler assembler) {

        return assembler.toResource(groupService.saveExternalGroup(newGroup));

    }

}

Repository: 库:

@RepositoryRestResource(excerptProjection = GroupSummary.class)
public interface GroupDao extends DefaultDao<Group, Long> {

    @NotNull
    List<Group> findByState(@Nullable GroupState state);
...other methods...

I would like to achieve to have possibility to go to /api/v2/groups and have there also link to /external. 我想实现有可能去/ api / v2 / groups并且还有链接到/ external。 Currently, only links from repository are returned: 目前,只返回来自存储库的链接:

"_links": {
    "first": {
      "href": "http://localhost:8300/api/v2/groups?page=0&size=20"
    },
    "self": {
      "href": "http://localhost:8300/api/v2/groups"
    },
    "next": {
      "href": "http://localhost:8300/api/v2/groups?page=1&size=20"
    },
    "last": {
      "href": "http://localhost:8300/api/v2/groups?page=1&size=20"
    },
    "profile": {
      "href": "http://localhost:8300/api/v2/profile/groups"
    },
    "search": {
      "href": "http://localhost:8300/api/v2/groups/search"
    }
  },

What should I implement to get there all as above plus something like this: 我应该如何实现上述所有内容以及类似的内容:

"external": {
          "href": "http://localhost:8300/api/v2/groups/external"
        }

Or is there problem with that "/external" is POST? 或者“/ external”是POST有问题吗? If so, please comment and consider this question with "method=GET". 如果是这样,请用“method = GET”评论并考虑这个问题。

Option 1: 选项1:

If it's a one-off, you could add the links in the controller method using the Resource class. 如果它是一次性的,您可以使用Resource类在控制器方法中添加链接。

@RequestMapping(value = "/external", method = POST)
public  @ResponseBody   PersistentEntityResource saveExternalGroup(
        @RequestBody Group newGroup,
        PersistentEntityResourceAssembler assembler) {

    PersistentEntityResource resource = assembler.toResource(groupService.saveExternalGroup(newGroup));

    // Replace with ControllerLinkBuilder call, or EntityLinks as you see fit.
    resource.add(new Link("http://localhost:8300/api/v2/groups/external","search"));

    return resource;
}

Option 2: 选项2:

If you would like this link to be added to every rendered Resource<Group> , then create a ResourceProcessor component to add it. 如果您希望将此链接添加到每个呈现的Resource<Group> ,则创建ResourceProcessor组件以添加它。

@Component
public class GroupResourceProcessor implements ResourceProcessor<Resource<Group>> {

    @Override
    public Resource<Group> process(Resource<Group> groupResource) {

        // Replace with ControllerLinkBuilder call, or EntityLinks as you see fit.
        groupResource.add(new Link("http://localhost:8300/api/v2/groups/external","search"));

        return groupResource;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM