将关联链接添加到 spring 数据休息自定义公开方法 [英] adding association links to spring data rest custom exposed method

查看:14
本文介绍了将关联链接添加到 spring 数据休息自定义公开方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经公开了一个自定义 @RepositoryRestController 以通过 Spring 数据公开自定义方法,其余方法的代码如下所示

Hi I have exposed a custom @RepositoryRestController to expose a custom method via Spring data rest the code for the method looks something like below

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

        Page<Foo> foos= fundRepository.searchByRsql(rsql, pageable);
        return pagedResourcesAssembler.toResource(foos);
    }

foo 实体

@Entity
@Table(name = "FOO_TBL", schema = "F")
@Data
public class Foo implements Identifiable<String> {
    @Id
    @Column(name = "ID")
    @Description("Id")
    private String id;
// associations
   @OneToMany(mappedBy = "foo", fetch = FetchType.LAZY)
    private List<FooFriends> fooFriends;

    @OneToMany(mappedBy = "foo", fetch = FetchType.LAZY)
    private List<Marks> marks;
}

Foo 实体可以很好地呈现来自自定义存储库方法的数据.但是 json 表示不包含指向实体关联的链接.有没有一种方法可以通过框架公开这些关联,而无需编写自定义 ResourceProcessor 就像他们在 spring 数据休息的其他开箱即用表示中所做的一样.

The Foo entity renders fine with the data coming out of the custom repository method .But the json representation does not include the links to the associations for the entity .Is there a way by which these associations can be exposed via the framework without writing custom ResourceProcessor like they do in other out of the box representations of spring data rest .

推荐答案

最后,我通过查看一些 spring 数据休息代码解决了这个问题.所以我从 AbstractRepositoryRestController 这个部分得到了实现这个的想法,这给了我一个想法如何同时使用 PersistentEntityResourceAssembler 和 pagedResourcesAssembler.

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);
        }
    }

因此,在将两者与 restm 方法中可用的 PersistentEntityResourceAssembler 一起使用后,我终于可以为我的自定义查询提供 spring 数据 rest HAl 类型表示.代码如下

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 :)

这篇关于将关联链接添加到 spring 数据休息自定义公开方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆