How to add links to root resource in Spring Data REST?

后端 未结 2 2038
广开言路
广开言路 2020-11-30 10:05

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

2条回答
  •  天命终不由人
    2020-11-30 10:43

    I have been searching for an answer to the same issue, but the key is: I don't have a controller. My url points to something created in an auth filter. What worked for me is to create a RootController that doesn't have any methods, and use it for building links in the ResourceProcessor implementation.

    @RestController
    @RequestMapping("/")
    public class RootController {}
    

    Then the link is inserted using the empty controller.

    @Component
    public class AuthLinkProcessor implements ResourceProcessor {
    
        @Override
        public RepositoryLinksResource process(RepositoryLinksResource resource) {
            resource.add(
                    linkTo(RootController.class)
                    .slash("auth/login")
                    .withRel("auth-login"));
            return resource;
        }
    }
    

提交回复
热议问题