diff --git a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/support/RepositoryRelProvider.java b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/support/RepositoryRelProvider.java index 9dd289667..e15c910dc 100644 --- a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/support/RepositoryRelProvider.java +++ b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/support/RepositoryRelProvider.java @@ -15,6 +15,9 @@ */ package org.springframework.data.rest.core.support; +import org.springframework.beans.factory.ObjectFactory; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; import org.springframework.data.rest.core.mapping.ResourceMappings; import org.springframework.hateoas.RelProvider; import org.springframework.util.Assert; @@ -24,16 +27,17 @@ import org.springframework.util.Assert; * * @author Oliver Gierke */ +@Order(Ordered.LOWEST_PRECEDENCE + 10) public class RepositoryRelProvider implements RelProvider { - private final ResourceMappings mappings; + private final ObjectFactory mappings; /** * Creates a new {@link RepositoryRelProvider} for the given {@link ResourceMappings}. * * @param mappings must not be {@literal null}. */ - public RepositoryRelProvider(ResourceMappings mappings) { + public RepositoryRelProvider(ObjectFactory mappings) { Assert.notNull(mappings, "ResourceMappings must not be null!"); this.mappings = mappings; @@ -45,7 +49,7 @@ public class RepositoryRelProvider implements RelProvider { */ @Override public String getCollectionResourceRelFor(Class type) { - return mappings.getMappingFor(type).getRel(); + return mappings.getObject().getMappingFor(type).getRel(); } /* @@ -54,7 +58,7 @@ public class RepositoryRelProvider implements RelProvider { */ @Override public String getItemResourceRelFor(Class type) { - return mappings.getMappingFor(type).getItemResourceRel(); + return mappings.getObject().getMappingFor(type).getItemResourceRel(); } /* @@ -63,6 +67,6 @@ public class RepositoryRelProvider implements RelProvider { */ @Override public boolean supports(Class delimiter) { - return mappings.hasMappingFor(delimiter); + return mappings.getObject().hasMappingFor(delimiter); } } diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java index 4932ef165..1bc4444ea 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java @@ -51,6 +51,7 @@ import org.springframework.data.rest.core.mapping.ResourceDescription; import org.springframework.data.rest.core.mapping.ResourceMappings; import org.springframework.data.rest.core.projection.ProxyProjectionFactory; import org.springframework.data.rest.core.support.DomainObjectMerger; +import org.springframework.data.rest.core.support.RepositoryRelProvider; import org.springframework.data.rest.core.util.UUIDConverter; import org.springframework.data.rest.webmvc.RepositoryRestController; import org.springframework.data.rest.webmvc.RepositoryRestHandlerAdapter; @@ -139,6 +140,11 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon return new Repositories(beanFactory); } + @Bean + public RepositoryRelProvider repositoryRelProvider(ObjectFactory resourceMappings) { + return new RepositoryRelProvider(resourceMappings); + } + @Bean @Qualifier public DefaultFormattingConversionService defaultConversionService() { @@ -427,7 +433,7 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon Repositories repositories = repositories(); RepositoryRestConfiguration config = config(); - return new ResourceMappings(config, repositories, getDefaultedRelProvider()); + return new ResourceMappings(config, repositories); } /** 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 e4be39458..e26ca2385 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 @@ -270,8 +270,13 @@ public abstract class AbstractWebIntegrationTests { protected void assertJsonPathDoesntExist(String path, MockHttpServletResponse response) throws Exception { try { - JsonPath.read(response.getContentAsString(), path); - fail(path + " should have failed"); + + Object result = JsonPath.read(response.getContentAsString(), path); + + if (result != null) { + fail("Was expecting to find no value for path " + path + " but got " + result.toString()); + } + } catch (InvalidPathException e) {} } 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 3db199df8..d549321fe 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 @@ -33,6 +33,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.rest.core.mapping.ResourceMappings; import org.springframework.data.rest.webmvc.AbstractWebIntegrationTests; import org.springframework.hateoas.Link; +import org.springframework.hateoas.RelProvider; import org.springframework.http.MediaType; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.context.ContextConfiguration; @@ -61,6 +62,7 @@ public class JpaWebTests extends AbstractWebIntegrationTests { @Autowired TestDataPopulator loader; @Autowired ResourceMappings mappings; + @Autowired RelProvider relProvider; ObjectMapper mapper = new ObjectMapper(); @@ -456,6 +458,14 @@ public class JpaWebTests extends AbstractWebIntegrationTests { assertJsonPathDoesntExist("$.lineItems", response); } + /** + * @see DATAREST-261 + */ + @Test + public void relProviderDetectsCustomizedMapping() { + assertThat(relProvider.getCollectionResourceRelFor(Person.class), is("people")); + } + /** * Asserts the {@link Person} resource the given link points to contains siblings with the given names. * @@ -466,7 +476,7 @@ public class JpaWebTests extends AbstractWebIntegrationTests { private void assertSiblingNames(Link link, String... siblingNames) throws Exception { String responseBody = request(link).getContentAsString(); - List persons = JsonPath.read(responseBody, "$._embedded.persons[*].firstName"); + List persons = JsonPath.read(responseBody, "$._embedded.people[*].firstName"); assertThat(persons, hasSize(siblingNames.length)); assertThat(persons, hasItems(siblingNames));