DATAREST-247 - RepositorySearchController now returns primitive results as is.

This commit is contained in:
Oliver Gierke
2014-03-11 19:25:14 +01:00
parent 572690b888
commit 31499b4ebf
5 changed files with 40 additions and 12 deletions

View File

@@ -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<Resources<?>> executeSearch(RootResourceInformation resourceInformation, WebRequest request,
public ResponseEntity<Object> 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<?>>(resources, HttpStatus.OK);
return new ResponseEntity<Object>(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<Link> links = new ArrayList<Link>();
@@ -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<String, String[]> parameters = request.getParameterMap();
Object result = invoker.invokeQueryMethod(method, parameters, pageable, null);
if (ClassUtils.isPrimitiveOrWrapper(method.getReturnType())) {
return result;
}
return resultToResources(result, assembler);
}

View File

@@ -119,7 +119,7 @@ public abstract class AbstractWebIntegrationTests {
protected List<Link> 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();

View File

@@ -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<Resources<?>> response = controller.executeSearch(resourceInformation, getRequest(parameters),
ResponseEntity<Object> response = controller.executeSearch(resourceInformation, getRequest(parameters),
"firstname", null, assembler);
ResourceTester tester = ResourceTester.of(response.getBody());

View File

@@ -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"));
}
}

View File

@@ -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<Profile, String> {
List<Profile> findByType(String type);
/**
* @see DATAREST-247
*/
long countByType(@Param("type") String type);
}