diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositorySearchController.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositorySearchController.java index 598c22c84..da8e01684 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositorySearchController.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositorySearchController.java @@ -148,7 +148,7 @@ class RepositorySearchController extends AbstractRepositoryRestController { * @return */ @ResponseBody - @RequestMapping(value = BASE_MAPPING + "/{method}", method = RequestMethod.GET, // + @RequestMapping(value = BASE_MAPPING + "/{search}", method = RequestMethod.GET, // produces = { "application/x-spring-data-compact+json" }) public ResourceSupport executeSearchCompact(RootResourceInformation resourceInformation, WebRequest request, @PathVariable String repository, @PathVariable String search, Pageable pageable) { @@ -173,7 +173,7 @@ class RepositorySearchController extends AbstractRepositoryRestController { links.add(resourceLink(resourceInformation, res)); } - return new Resource(EMPTY_RESOURCE_LIST, links); + return new Resources>(EMPTY_RESOURCE_LIST, links); } /** diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AbstractWebIntegrationTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AbstractWebIntegrationTests.java index 1f303592e..123fa3958 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AbstractWebIntegrationTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AbstractWebIntegrationTests.java @@ -94,6 +94,10 @@ public abstract class AbstractWebIntegrationTests { return request(link.expand().getHref()); } + protected MockHttpServletResponse request(Link link, MediaType mediaType) throws Exception { + return request(link.expand().getHref(), mediaType); + } + protected MockHttpServletResponse request(String href) throws Exception { return request(href, DEFAULT_MEDIA_TYPE); } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java index 2aed654c3..c182adc70 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/jpa/JpaWebTests.java @@ -27,6 +27,8 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import net.minidev.json.JSONArray; + import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -467,6 +469,35 @@ public class JpaWebTests extends AbstractWebIntegrationTests { assertThat(relProvider.getCollectionResourceRelFor(Person.class), is("people")); } + /** + * @see DATAREST-311 + */ + @Test + public void onlyLinksShouldAppearWhenExecuteSearchCompact() throws Exception { + + Link peopleLink = discoverUnique("people"); + Person daenerys = new Person("Daenerys", "Targaryen"); + String daenerysString = mapper.writeValueAsString(daenerys); + + MockHttpServletResponse createdPerson = postAndGet(peopleLink, daenerysString, MediaType.APPLICATION_JSON); + Link daenerysLink = assertHasLinkWithRel("self", createdPerson); + assertJsonPathEquals("$.firstName", "Daenerys", createdPerson); + + Link searchLink = discoverUnique(peopleLink, "search"); + Link byFirstNameLink = discoverUnique(searchLink, "findFirstPersonByFirstName"); + + MockHttpServletResponse response = request(byFirstNameLink.expand("Daenerys"), + MediaType.parseMediaType("application/x-spring-data-compact+json")); + + String responseBody = response.getContentAsString(); + + JSONArray personLinks = JsonPath. read(responseBody, "$.links[?(@.rel=='person')].href"); + + assertThat(personLinks, hasSize(1)); + assertThat(personLinks.get(0), is((Object) daenerysLink.getHref())); + assertThat(JsonPath. read(responseBody, "$.content"), hasSize(0)); + } + /** * Asserts the {@link Person} resource the given link points to contains siblings with the given names. *