adding association links to spring data rest custom exposed method

六月ゝ 毕业季﹏ 提交于 2019-12-06 05:51:56

Finally this got solved by me loooking at some spring data rest code .So the idea to implement this I got from the AbstractRepositoryRestController this section which gave me an idea on how to use both PersistentEntityResourceAssembler and pagedResourcesAssembler together.

@SuppressWarnings({ "unchecked" })
    protected Resources<?> toResources(Iterable<?> source, PersistentEntityResourceAssembler assembler,
            Class<?> domainType, Link baseLink) {

        if (source instanceof Page) {
            Page<Object> page = (Page<Object>) source;
            return entitiesToResources(page, assembler, domainType, baseLink);
        } else if (source instanceof Iterable) {
            return entitiesToResources((Iterable<Object>) source, assembler, domainType);
        } else {
            return new Resources(EMPTY_RESOURCE_LIST);
        }
    }

so after using both with PersistentEntityResourceAssembler available in the restm method I finally could give spring data rest HAl type representaiton for my custom queries .Code below

@RequestMapping(method = RequestMethod.GET, value = "/foo/rsqlsearch")
    public @ResponseBody PagedResources<?> findAllPaged(@RequestParam(value = RSQL_REL) String rsql,
                                                        Pageable pageable,
                                                        PersistentEntityResourceAssembler eass) {

        Page<Object> entities = (Page<Object>) repository.searchByRsql(rsql, pageable);
        return assembler.toResource(entities, eass);
    }

Hope it helps people around :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!