Spring Data Rest - Add Link to Search Endpoint

In our Spring-Data-Rest project, do we have a custom (fuzzy) endpoint search / buergers / search / findBuergerFuzzy? searchString = "...".

Is it possible to add a link for it to the endpoint / buergers / search (Without overriding the automatically exposed Repository search methods)?

Controller displaying search:

@BasePathAwareController @RequestMapping("/buergers/search/") public class BuergerSearchController { @Autowired QueryService service; @RequestMapping(value = "/findBuergerFuzzy", method = RequestMethod.GET) public @ResponseBody ResponseEntity<?> findBuergerFuzzy(PersistentEntityResourceAssembler assembler, @Param("searchString") String searchString) { if (searchString.length() < 3) throw new IllegalArgumentException("Search String must be at least 3 chars long."); List<Buerger> list = service.query(searchString, Buerger.class, new String[]{"vorname", "nachname", "geburtsdatum", "augenfarbe"}); final List<PersistentEntityResource> collect = list.stream().map(assembler::toResource).collect(Collectors.toList()); return new ResponseEntity<Object>(new Resources<>(collect), HttpStatus.OK); } } 
+5
source share
1 answer

Digging through the source spring -data-rest source, I found a RepositorySearchesResource which seems to solve the problem.

 @Component public class SearchResourcesProcessor implements ResourceProcessor<RepositorySearchesResource> { @Override public RepositorySearchesResource process(RepositorySearchesResource repositorySearchesResource) { final String search = repositorySearchesResource.getId().getHref(); final Link findFullTextFuzzy = new Link(search + "/findFullTextFuzzy{?q}").withRel("findFullTextFuzzy"); repositorySearchesResource.add(findFullTextFuzzy); return repositorySearchesResource; } } 

Since we generate this code using templates, it sufficiently and fully meets our needs. Be sure to check the comments for the correct and safe way.

+3
source

Source: https://habr.com/ru/post/1232253/


All Articles