2

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

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

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 .

Gaurav Rawat
  • 1,294
  • 1
  • 25
  • 52
  • Are child entities (`FooFriends` and `Marks`) also exposed via `JpaRepository`? – nKognito Jul 31 '15 at 13:28
  • @nKognito yes they are ,also they do render fine with out of the box methods9(in spring data rest ) like /foos issue is with custom method or endpoint – Gaurav Rawat Jul 31 '15 at 14:35
  • try to remove lombok's annotations – nKognito Jul 31 '15 at 18:16
  • Hmm not sure that will work in this case as the spring data out of the box representations are coming out fine. I think in need to fund a way to use the PersistentEntityResourceAssembler with the paging assembler . – Gaurav Rawat Jul 31 '15 at 18:36

1 Answers1

4

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

Gaurav Rawat
  • 1,294
  • 1
  • 25
  • 52
  • Thank you for this, this is an infuriating problem for those of use who want consistency across the API. Why should custom methods not follow the expected HAL convention? I am going to try to turn this into a reusable component for writing repository extensions. – Will Faithfull Jul 14 '16 at 12:32