且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Spring Data REST 中添加指向根资源的链接?

更新时间:2022-03-26 06:59:25

这可以通过实现 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"
}

到您的根列表链接