DATAREST-31 - Search links are now rendered as link templates.

MethodResourceMapping now exposes the parameters a query method expects. The RepositorySearchController the uses these to append the 

Upgraded to Spring Data Commons 1.7.0.BUILD-SNAPSHOT.
This commit is contained in:
Oliver Gierke
2014-01-17 13:28:10 +01:00
parent 5ff6457911
commit 4b6f0f620a
7 changed files with 116 additions and 22 deletions

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.core.mapping;
import java.lang.reflect.Method;
import java.util.List;
/**
* A {@link ResourceMapping} that is backed by a {@link Method}.
@@ -30,4 +31,11 @@ public interface MethodResourceMapping extends ResourceMapping {
* @return
*/
Method getMethod();
/**
* Returns the names of the parameters the method exposes.
*
* @return
*/
List<String> getParameterNames();
}

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,10 +16,17 @@
package org.springframework.data.rest.core.mapping;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.Path;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.hateoas.core.AnnotationAttribute;
import org.springframework.hateoas.core.MethodParameters;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -30,11 +37,15 @@ import org.springframework.util.StringUtils;
*/
class RepositoryMethodResourceMapping implements MethodResourceMapping {
private static final AnnotationAttribute PARAM_VALUE = new AnnotationAttribute(Param.class);
private final boolean isExported;
private final String rel;
private final Path path;
private final Method method;
private final List<String> parameterNames;
/**
* Creates a new {@link RepositoryMethodResourceMapping} for the given {@link Method}.
*
@@ -53,6 +64,21 @@ class RepositoryMethodResourceMapping implements MethodResourceMapping {
this.path = annotation == null || !StringUtils.hasText(annotation.path()) ? new Path(method.getName()) : new Path(
annotation.path());
this.method = method;
this.parameterNames = discoverParameterNames(method);
}
private static final List<String> discoverParameterNames(Method method) {
List<String> result = new ArrayList<String>();
for (MethodParameter parameter : new MethodParameters(method, PARAM_VALUE).getParameters()) {
String name = parameter.getParameterName();
if (name != null) {
result.add(name);
}
}
return Collections.unmodifiableList(result);
}
/*
@@ -90,4 +116,13 @@ class RepositoryMethodResourceMapping implements MethodResourceMapping {
public Method getMethod() {
return method;
}
/*
* (non-Javadoc)
* @see org.springframework.data.rest.core.mapping.MethodResourceMapping#getParameterNames()
*/
@Override
public List<String> getParameterNames() {
return parameterNames;
}
}

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.
@@ -15,20 +15,20 @@
*/
package org.springframework.data.rest.core.mapping;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.lang.reflect.Method;
import org.junit.Test;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.Path;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.data.rest.core.mapping.RepositoryCollectionResourceMapping;
import org.springframework.data.rest.core.mapping.RepositoryMethodResourceMapping;
import org.springframework.data.rest.core.mapping.ResourceMapping;
/**
* Unit tests for {@link RepositoryMethodResourceMapping}.
*
* @author Oliver Gierke
*/
public class RepositoryMethodResourceMappingUnitTests {
@@ -36,7 +36,7 @@ public class RepositoryMethodResourceMappingUnitTests {
RepositoryCollectionResourceMapping resourceMapping = new RepositoryCollectionResourceMapping(PersonRepository.class);
@Test
public void foo() throws Exception {
public void defaultsMappingToMethodName() throws Exception {
Method method = PersonRepository.class.getMethod("findByLastname", String.class);
ResourceMapping mapping = new RepositoryMethodResourceMapping(method, resourceMapping);
@@ -53,6 +53,31 @@ public class RepositoryMethodResourceMappingUnitTests {
assertThat(mapping.getPath(), is(new Path("bar")));
}
/**
* @see DATAREST-31
*/
@Test
public void doesNotDiscoverAnyParametersIfNotAnnotated() throws Exception {
Method method = PersonRepository.class.getMethod("findByLastname", String.class);
MethodResourceMapping mapping = new RepositoryMethodResourceMapping(method, resourceMapping);
assertThat(mapping.getParameterNames(), is(emptyIterable()));
}
/**
* @see DATAREST-31
*/
@Test
public void resolvesParameterNamesIfNotAnnotated() throws Exception {
Method method = PersonRepository.class.getMethod("findByFirstname", String.class);
MethodResourceMapping mapping = new RepositoryMethodResourceMapping(method, resourceMapping);
assertThat(mapping.getParameterNames(), hasSize(1));
assertThat(mapping.getParameterNames(), hasItem("firstname"));
}
static class Person {}
interface PersonRepository extends Repository<Person, Long> {
@@ -60,7 +85,7 @@ public class RepositoryMethodResourceMappingUnitTests {
Iterable<Person> findByLastname(String lastname);
@RestResource(path = "/bar")
Iterable<Person> findByFirstname(String firstname);
Iterable<Person> findByFirstname(@Param("firstname") String firstname);
@RestResource(path = "foo")
Iterable<Person> findByEmailAddress(String email);