1

By default, in Spring Data Rest the @Id of the entity is not exposed. In line with the REST rules, we're supposed to use the URI of the resource to refer to it. Given this assumption, the findBy queries should work if you pass a URI to them, but they don't.

For example, say I have a one-to-many relationship between Teacher and Student. I want to find students by teacher.

List<Student> findByTeacher(Teacher teacher)

http://localhost:8080/repositories/students/search/findByTeacher?teacher=http://localhost:8080/repositories/teachers/1

This doesn't work because the framework is attempting to convert the teacher URI to a Long. I get this error that says "Failed to convert from type java.lang.String to type java.lang.Long".

Am I missing something?

ivan
  • 11
  • 3
  • is this still actual? I have the same issue.. http://stackoverflow.com/questions/29897351/the-way-of-passing-complex-params-into-spring-date-rests-method/29914129#29914129 – nKognito Apr 29 '15 at 10:29

2 Answers2

2
  1. You could expose @Id s by configuring web intializer

    //Web intializer @Configuration public static class RespositoryConfig extends RepositoryRestMvcConfiguration { @Override protected void configureRepositoryRestConfiguration( RepositoryRestConfiguration config) { config.exposeIdsFor(Teacher.class); } }

  2. Its good to change List to Page

    List findByTeacher(Teacher teacher)

to

Page<Student> findByTeacher(@Param("teacher) Teacher teacher, Pageable pageable);

Also note @Param annotation is required along with Pageable. The latter is required because return type "Page"

3.Latest snapshots, not milestones work fine

Stackee007
  • 3,196
  • 1
  • 26
  • 39
  • 1. That's a good workaround. 2. Good point. 3. I'm using the latest snapshots and it's NOT working. – ivan Jan 28 '14 at 01:18
  • I am using latest Snapshots and it works for me. May be problem is somewhere else? Trace? – Stackee007 Jan 28 '14 at 14:50
  • `ERROR AbstractRepositoryRestController - Failed to convert from type java.lang.String to type java.lang.Long for value 'http://localhost:8080/repositories/teachers/1'` – ivan Jan 30 '14 at 03:54
1

See https://jira.spring.io/browse/DATAREST-502

Depending of your version of Spring Data, it would work as you want or not. If you are with Spring Data 2.4, you need to pass the URI. If you are with a previous version, you need to pass the id.

JR Utily
  • 1,792
  • 1
  • 23
  • 38