Andy
Andy

Reputation: 8918

How to add @RepositoryRestController methods to API listing?

I have a controller that looks something like this:

@RepositoryRestController
public class PersonOmnisearchController {

    @Autowired
    private PersonRepository personRepository;

    @ResponseBody
    @RequestMapping(value = "/persons/search/personOmnisearch", method = RequestMethod.GET)
    public ResponseEntity<?> personOmnisearch(@RequestParam("search") String search, PersistentEntityResourceAssembler assembler) {
        Person p = personRepository.findOne(Long.valueOf(search));
        if (p == null) {
            p = personRepository.findBySsn(search);
        }
        if (p == null) {
            throw new ResourceNotFoundException();
        }
        return ResponseEntity.ok(assembler.toResource(p));
    }
}

It works great, but when using Traverson to fetch the data, there is no personOmnisearch link in the /api/persons/search's _links section. How do I let Spring Data REST know about my controller to put it in the api so it can be navigated to without having to know the direct URL?

Here's my Traverson code in case it helps:

api.json()
   .follow('$._links.persons.href', '$._links.search.href', 'omniSearch')
   .withTemplateParameters({
       search: search
   })
   .getResource()

P.S. - I'm not sure if this is a Spring Data REST or HATEOAS question, so I tagged both. Feel free to remove whichever one is irrelevant.

Upvotes: 0

Views: 566

Answers (1)

Oliver Drotbohm
Oliver Drotbohm

Reputation: 83141

You might wanna implement a ResourceProcessor<RepositorySearchesResource> and in its callback method process(…) check the given RepositorySearchesResource's domain type for the one you're interested in and add the link to the controller method.

If the implementation is registered as Spring Bean, it will get called for every access of the list of available searches.

Upvotes: 1

Related Questions