Reputation: 11
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?
Upvotes: 1
Views: 1463
Reputation: 1842
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.
Upvotes: 1
Reputation: 3244
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); } }
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
Upvotes: 2