Spring Data Rest - 添加指向搜索端点的链接

Pet*_*ler 5 spring spring-data-rest spring-hateoas

在我们的Spring-Data-Rest项目中,我们在/ buergers/search/findBuergerFuzzy?searchString ="..."端点上进行了自定义(模糊)搜索.

是否可以在/ buergers/search端点上添加链接(不覆盖自动公开的Repository findBy方法)?

控制器暴露搜索:

@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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Pet*_*ler 5

挖掘spring-data-rest源我找到了RepositorySearchesResource,它似乎解决了这个问题.

@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;
    }
}
Run Code Online (Sandbox Code Playgroud)

因为我们通过模板生成此代码,所以这足以满足我们的需求.确保以正确和安全的方式检查评论.

  • 您应该检查`resource.getDomainType()`以确保您的搜索功能仅显示在正确的资源上.`如果(ResourceClass.class.equals(resource.getDomainType()))` (2认同)
  • 你的解决方案非常危险,因为你的方法映射(它在哪里服务)和它的链接(跟随关系将导致)之间失去了一致性.您应该使用Spring Hateoas中的`linkTo`和`methodOn`静态方法来避免在创建"链接"时使用它们 (2认同)

kya*_*kya 2

版本

迁移到 1.0.changes

ResourceSupport 现在是 RepresentationModel

资源现在是 EntityModel

资源现在是 CollectionModel

PagedResources 现在是 PagedModel

代码

新版本代码:

import org.springframework.data.rest.webmvc.RepositorySearchesResource;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.server.RepresentationModelProcessor;
import org.springframework.stereotype.Component;

@Component
public class RepositorySearchesProcessor implements RepresentationModelProcessor<RepositorySearchesResource> {

    @Override
    public RepositorySearchesResource process(RepositorySearchesResource model) {
        System.out.println(model.getDomainType());
        model.add(Link.of(model.getRequiredLink("self").getHref() + "/findFullTextFuzzy{?q}").withRel("findFullTextFuzzy"));
        return model;
    }
}
Run Code Online (Sandbox Code Playgroud)

如何

关于如何找到你使用的资源或模型,在每个方法中设置断点后RepresentationModel,你可能会发现一些有用的东西:

在此输入图像描述