1

How can we make spring-data-rest return a single resource for a query, instead of an embedded list? This would make navigation much more intuitive for the client.

e.g.

t.follow("search", "byNameAndType", "identity", "mainContact")

instead of

t.follow("search", "byNameAndType", "$_embedded.credentials[0]._links.identity.href", "mainContact")

I have a JPA repository that always return 1 or 0 results. The uniqueness is enforced by a database contraint.

@RepositoryRestResource
public interface CredentialRepository extends PagingAndSortingRepository<Credential, Long>,
    ExternalIdRepository<Credential, Long> {


@RestResource(path = "byNameAndType", rel = "byNameAndType")
Credential findByNameIgnoreCaseAndTypeIgnoreCase(@Param("name") String name, @Param("type") String type);

}

spring-data-rest instead of returning the single credential object, it returns an embedded list.

$ curl -H "Accept: text/plain, application/hal+json, */*" -H "X-VCIDB-User-Id:testUser" http://127.0.0.1:8090/credentials/search/byNameAndType?name=V6UqkSG8\&type=myType
{
  "_embedded" : {
"credentials" : [ {
  "version" : 1,
  "lastUpdTs" : "2014-11-13T12:08:49.301+13:00",
  "lastUpdBy" : ":integration-test",
  "createdTs" : "2014-11-13T12:08:49.092+13:00",
  etc etc

instead, it should return the same as if i retrieved it by primary key.

{
  "version" : 1,
  "lastUpdTs" : "2014-11-13T12:08:49.301+13:00",
  etc etc

Otherwise the API to the client looks horrible

    Traverson t = new Traverson(new URI("http://127.0.0.1:8090/credentials"), MediaTypes.HAL_JSON);
    t.setRestOperations(template);
    Map<String, Object> params = new HashMap<>();
    params.put("name", "V6UqkSG8");
    params.put("type", "myType");
    String contactUuid = t.follow("search", "byNameAndType", "$_embedded.credentials[0]._links.identity.href", "mainContact")
            .withTemplateParameters(params).<String> toObject("$.uuid");
    LOG.info(contactUuid);
    assertThat(contactUuid, Matchers.is("7a7faeaf-6da3-4188-9d28-afbb30ce38b3"));

when the traversion would be much easier to understand for the client if it was:

String contactUuid = t.follow("search", "byNameAndType", "identity", "mainContact")
            .withTemplateParameters(params).<String> toObject("$.uuid");
dan carter
  • 4,158
  • 1
  • 33
  • 34

0 Answers0