如何在 Spring Data REST 中添加指向根资源的链接? [英] How to add links to root resource in Spring Data REST?

查看:28
本文介绍了如何在 Spring Data REST 中添加指向根资源的链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Spring Data REST 的资源根列表中公开外部资源(不通过存储库管理)?我按照 Restbucks

How to expose an external resource (not managed through a repository) in the root listing of resources of Spring Data REST? I defined a controller following the pattern in Restbucks

推荐答案

这可以通过实现 ResourceProcessor 来完成.

This can be done by implementing ResourceProcessor<RepositoryLinksResource>.

以下代码片段将/others"添加到根列表

Following code snippet adds "/others" to the root listing

@Controller
@ExposesResourceFor(Other.class)
@RequestMapping("/others")
public class CustomRootController implements
        ResourceProcessor<RepositoryLinksResource> {

    @ResponseBody
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<Resources<Resource<Other>>> listEntities(
            Pageable pageable) throws ResourceNotFoundException {
            //... do what needs to be done
    }

    @Override
    public RepositoryLinksResource process(RepositoryLinksResource resource) {
        resource.add(ControllerLinkBuilder.linkTo(CustomRootController.class).withRel("others"));

        return resource;
    }
}

应该加上

{
    "rel": "others",
    "href": "http://localhost:8080/api/others"
}

到您的根列表链接

这篇关于如何在 Spring Data REST 中添加指向根资源的链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆