【问题标题】:Spring Data Rest - Add link to search endpointSpring Data Rest - 添加链接到搜索端点
【发布时间】:2015-12-23 02:05:34
【问题描述】:

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

是否可以在 /buergers/search 端点上为其添加链接(不覆盖自动公开的存储库 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);
    }
}

【问题讨论】:

    标签: spring spring-data-rest spring-hateoas


    【解决方案1】:

    更新:这是一个过时的解决方法答案。升级到 Spring HATEOAS 1.0。


    旧的解决方法

    挖掘 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;
        }
    }
    

    因为我们通过模板生成这段代码,这就足够了,满足了我们的需求。请务必检查 cmets 是否正确且安全。

    【讨论】:

    • 您应该检查resource.getDomainType() 以确保您的搜索功能仅显示在正确的资源上。 if(ResourceClass.class.equals(resource.getDomainType()))
    • 您的解决方案非常危险,因为您的方法映射(它在哪里提供服务)和它的链接(跟随关系将导致)之间失去一致性。您应该使用 Spring Hateoas 中的 linkTomethodOn 静态方法来避免在创建 Link 时出现这种情况
    • 答案太老了
    【解决方案2】:

    版本

    migrate-to-1.0.changes

    ResourceSupport 现在是 RepresentationModel

    资源现在是 EntityModel

    Resources 现在是 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;
        }
    }
    

    如何

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

    【讨论】:

      猜你喜欢
      • 2016-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-11
      • 2015-11-13
      • 2016-03-06
      • 2015-03-23
      • 2017-06-21
      相关资源
      最近更新 更多