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 05e4dcaef..55d3901ce 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 @@ -41,6 +41,7 @@ import org.springframework.hateoas.Resources; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @@ -127,13 +128,13 @@ class RepositorySearchController extends AbstractRepositoryRestController { */ @ResponseBody @RequestMapping(value = BASE_MAPPING + "/{search}", method = RequestMethod.GET) - public ResponseEntity> executeSearch(RootResourceInformation resourceInformation, WebRequest request, + public ResponseEntity executeSearch(RootResourceInformation resourceInformation, WebRequest request, @PathVariable String search, Pageable pageable, PersistentEntityResourceAssembler assembler) { Method method = checkExecutability(resourceInformation, search); - Resources resources = executeQueryMethod(resourceInformation.getInvoker(), request, method, pageable, assembler); + Object resources = executeQueryMethod(resourceInformation.getInvoker(), request, method, pageable, assembler); - return new ResponseEntity>(resources, HttpStatus.OK); + return new ResponseEntity(resources, HttpStatus.OK); } /** @@ -153,8 +154,7 @@ class RepositorySearchController extends AbstractRepositoryRestController { PersistentEntityResourceAssembler assembler) { Method method = checkExecutability(resourceInformation, search); - ResourceSupport resource = executeQueryMethod(resourceInformation.getInvoker(), request, method, pageable, - assembler); + Object resource = executeQueryMethod(resourceInformation.getInvoker(), request, method, pageable, assembler); List links = new ArrayList(); @@ -209,12 +209,16 @@ class RepositorySearchController extends AbstractRepositoryRestController { * @param pageable * @return */ - private Resources executeQueryMethod(final RepositoryInvoker invoker, WebRequest request, Method method, + private Object executeQueryMethod(final RepositoryInvoker invoker, WebRequest request, Method method, Pageable pageable, PersistentEntityResourceAssembler assembler) { Map parameters = request.getParameterMap(); Object result = invoker.invokeQueryMethod(method, parameters, pageable, null); + if (ClassUtils.isPrimitiveOrWrapper(method.getReturnType())) { + return result; + } + return resultToResources(result, assembler); } 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 e26ca2385..1f303592e 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 @@ -119,7 +119,7 @@ public abstract class AbstractWebIntegrationTests { protected List discover(Link root, String rel) throws Exception { - MockHttpServletResponse response = mvc.perform(get(root.getHref()).accept(DEFAULT_MEDIA_TYPE)).// + MockHttpServletResponse response = mvc.perform(get(root.expand().getHref()).accept(DEFAULT_MEDIA_TYPE)).// andExpect(status().isOk()).// andExpect(hasLinkWithRel(rel)).// andReturn().getResponse(); @@ -130,7 +130,7 @@ public abstract class AbstractWebIntegrationTests { protected Link discoverUnique(Link root, String rel) throws Exception { - MockHttpServletResponse response = mvc.perform(get(root.getHref()).accept(DEFAULT_MEDIA_TYPE)).// + MockHttpServletResponse response = mvc.perform(get(root.expand().getHref()).accept(DEFAULT_MEDIA_TYPE)).// andExpect(status().isOk()).// andExpect(hasLinkWithRel(rel)).// andReturn().getResponse(); diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositorySearchControllerIntegrationTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositorySearchControllerIntegrationTests.java index f01a086c5..32974e327 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositorySearchControllerIntegrationTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositorySearchControllerIntegrationTests.java @@ -30,7 +30,6 @@ import org.springframework.data.rest.webmvc.jpa.Person; import org.springframework.data.rest.webmvc.jpa.TestDataPopulator; import org.springframework.hateoas.PagedResources; import org.springframework.hateoas.ResourceSupport; -import org.springframework.hateoas.Resources; import org.springframework.http.ResponseEntity; import org.springframework.test.context.ContextConfiguration; import org.springframework.transaction.annotation.Transactional; @@ -86,7 +85,7 @@ public class RepositorySearchControllerIntegrationTests extends AbstractControll RequestParameters parameters = new RequestParameters("firstName", "John"); RootResourceInformation resourceInformation = getResourceInformation(Person.class); - ResponseEntity> response = controller.executeSearch(resourceInformation, getRequest(parameters), + ResponseEntity response = controller.executeSearch(resourceInformation, getRequest(parameters), "firstname", null, assembler); ResourceTester tester = ResourceTester.of(response.getBody()); diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mongodb/MongoWebTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mongodb/MongoWebTests.java index aafd96b6a..e692e66e1 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mongodb/MongoWebTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mongodb/MongoWebTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 the original author or authors. + * Copyright 2013-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package org.springframework.data.rest.webmvc.mongodb; import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import java.util.Arrays; @@ -26,6 +27,7 @@ import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.rest.webmvc.AbstractWebIntegrationTests; import org.springframework.hateoas.Link; +import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.context.ContextConfiguration; /** @@ -95,4 +97,21 @@ public class MongoWebTests extends AbstractWebIntegrationTests { follow(userLink).// andExpect(jsonPath("$.address.zipCode").value(is(notNullValue()))); } + + /** + * @see DATAREST-247 + */ + @Test + public void executeQueryMethodWithPrimitiveReturnType() throws Exception { + + Link profiles = discoverUnique("profiles"); + Link profileSearches = discoverUnique(profiles, "search"); + Link countByTypeLink = discoverUnique(profileSearches, "countByType"); + + assertThat(countByTypeLink.isTemplated(), is(true)); + assertThat(countByTypeLink.getVariableNames(), hasItem("type")); + + MockHttpServletResponse response = request(countByTypeLink.expand("Twitter")); + assertThat(response.getContentAsString(), is("1")); + } } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mongodb/ProfileRepository.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mongodb/ProfileRepository.java index c09e5b5f2..10552c059 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mongodb/ProfileRepository.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/mongodb/ProfileRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ package org.springframework.data.rest.webmvc.mongodb; import java.util.List; import org.springframework.data.repository.PagingAndSortingRepository; +import org.springframework.data.repository.query.Param; /** * @author Jon Brisbin @@ -26,4 +27,9 @@ import org.springframework.data.repository.PagingAndSortingRepository; public interface ProfileRepository extends PagingAndSortingRepository { List findByType(String type); + + /** + * @see DATAREST-247 + */ + long countByType(@Param("type") String type); }