0

I am trying to find any info regarding the following issue: I have a JPA repository which was exposed to REST service with Spring Data Rest project:

Page<Business> findByUser(@Param("user") User user, Pageable pageable);

where the user parameter is the entity.

Now when I try to call this REST method from client (with RestTemplate or Traverson) I can't understand how should I pass this param...

Is it possible to implement it without making custom controller?

Thank you in advance

nKognito
  • 6,297
  • 17
  • 77
  • 138

2 Answers2

0

I am assuming User is another Spring Data REST managed entity? If so you pass a URI identifying the user you'd like to hand into the method to the query method resource.

Traverson traverson = new Traverson(…);
traverson.follow("businesses", "search", "findByUser")
  .withTemplateParameters(Collections.singletonMap("user", "…/users/4711")).…
Oliver Drotbohm
  • 80,157
  • 18
  • 225
  • 211
  • The problem that once I use the format you suggested rest call fails with: `Failed to convert from type java.lang.String to type java.lang.Long for value '.../users/4'; nested exception is java.lang.NumberFormatException: For input string: ".../users/4"` – nKognito Apr 29 '15 at 09:52
  • The same issue - http://stackoverflow.com/questions/21268063/findby-uri-not-working-in-spring-data-rest – nKognito Apr 29 '15 at 10:02
  • I have the same issue (not using Traverson, but directly hitting my server API). So I did a little debug, and I've watch what is in `org.springframework.data.repository.support.DomainClassConverter`. Inside, `info.metadata.idType` is `Long` (which is normal, my @Id is put on a Long), while source is the URI String. And so it try to call for a String -> Long Converter. And this provoke the exception. I think it is not intended to arrive here, but I can not understand what went wrong on the road... – JR Utily Aug 12 '15 at 08:47
  • and so, if I put the DB id instead of the URI, it does work. But I thought it was not intended to manipulate them. – JR Utily Aug 12 '15 at 08:57
  • by the way, I've put a breakpoint in `org.springframework.data.rest.core.UriToEntityConverter` and it never hit. So may be a problem with the registration of this converter ? – JR Utily Aug 12 '15 at 09:52
0

I finally figured it out. Look at this issue on Spring JIRA.

The way to request this depends on your version of Spring Data Rest. If you have a version 2.4+, you have to use the URI. If you have an older version, you have to use the primary key.

So you may need to expose your primary keys if you have an older version of SDR. This question could help you in managing that.

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