I have a Spring Data Rest Repository controller that utilizes JPA for the query implementation, and I need to add some custom query methods that cannot be done using the standard queryByExample method that JPA supports. I have created an Impl class that has the necessary method, but I cannot get it to be recognized. I saw that I can utilize a standard Spring MVC Controller, but I want to have a unified API, and basically all I really want is to implement my own custom /search methods.
Even with the custom controller, the problem is then that the HAL links and other related items are no longer provided.
Can the Spring folks spend some time having someone document how to do some of this more advanced stuff? I'm guessing that having to implement your own search methods at times are fairly common, and it would be time well spent to make it clear how to do this.
A simple implementation could look like this:
@BasePathAwareController
class CustomInvocationsController implements ResourceProcessor<RepositorySearchesResource> {
private final YourRepository repository;
public CustomInvocationsController(YourRepository repository) {
this.repository = repository;
}
@RequestMapping(…)
ResponseEntity<?> handleRequest(PersistentEntityResourceAssembler assembler)
// invoke repository
// Use assembler to build a representation
// return ResponseEntity
}
@Override
public RepositorySearchesResource process(RepositorySearchesResource resource) {
// add Link to point to the custom handler method
}
}
A few things to note:
@BasePathAwareController
instead of a plain @Controller
makes sure whatever you're mapping the handler method to, it will consider the base path you've configured on Spring Data REST, too.PersistentEntityResourceAssembler
basically abstracts setting up a representation model within a PersistentEntityResource
so that the Spring Data REST specific treatment of associations etc. kicks in (associations becoming links etc.ResourceProcessor
to post-process RepositorySearchesResource
which is returned for the resource rendering all searches. Currently, there's no way to determine which domain type that resource was rendered. I filed and fixed DATAREST-515 to improve that.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With