From 6c5bab7be330e565cccb241094ea8e2c233775ed Mon Sep 17 00:00:00 2001 From: Jon Brisbin Date: Mon, 7 May 2012 13:28:04 -0500 Subject: [PATCH] Changed the way I look for ConversionServices to add to the default set of Converters by looking in the ApplicationContext rather than playing monkey games with @Configuration and @Autowired. --- .../rest/webmvc/RepositoryRestController.java | 5 +++ .../RepositoryRestMvcConfiguration.java | 7 ----- .../spec/RepositoryRestControllerSpec.groovy | 2 +- .../data/rest/test/webmvc/UuidTest.java | 31 +++++++++++++++++++ .../rest/test/webmvc/UuidTestRepository.java | 11 +++++++ .../test/resources/META-INF/persistence.xml | 1 + .../spring-data-rest/repositories-export.xml | 2 -- 7 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/UuidTest.java create mode 100644 spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/UuidTestRepository.java diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestController.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestController.java index 20e849103..000da40b5 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestController.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestController.java @@ -20,6 +20,7 @@ import java.util.concurrent.atomic.AtomicReference; import org.codehaus.jackson.map.ObjectMapper; import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.InitializingBean; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; @@ -214,6 +215,10 @@ public class RepositoryRestController @SuppressWarnings({"unchecked"}) @Override public void afterPropertiesSet() throws Exception { + for (ConversionService convsvc : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, + ConversionService.class).values()) { + conversionService.addConversionServices(convsvc); + } } @SuppressWarnings({"unchecked"}) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestMvcConfiguration.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestMvcConfiguration.java index 8c61854b6..51d9245c0 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestMvcConfiguration.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestMvcConfiguration.java @@ -11,7 +11,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; -import org.springframework.core.convert.ConversionService; import org.springframework.data.rest.repository.RepositoryExporter; import org.springframework.data.rest.repository.context.ValidatingRepositoryEventListener; import org.springframework.data.rest.repository.jpa.JpaRepositoryExporter; @@ -46,9 +45,6 @@ public class RepositoryRestMvcConfiguration { @Autowired(required = false) JpaRepositoryExporter customJpaRepositoryExporter; - @Autowired(required = false) - ConversionService customConversionService; - @Autowired(required = false) List> httpMessageConverters = new ArrayList>(); @@ -118,9 +114,6 @@ public class RepositoryRestMvcConfiguration { .repositoryExporters(Arrays.asList(jpaRepositoryExporter())) .httpMessageConverters(httpMessageConverters()) .jsonMediaType("application/json"); - if (null != customConversionService) { - repositoryRestController.conversionService(customConversionService); - } } return repositoryRestController; } diff --git a/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/RepositoryRestControllerSpec.groovy b/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/RepositoryRestControllerSpec.groovy index 76763fb66..90c5c5bc6 100644 --- a/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/RepositoryRestControllerSpec.groovy +++ b/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/RepositoryRestControllerSpec.groovy @@ -86,7 +86,7 @@ class RepositoryRestControllerSpec extends Specification { then: model.status == HttpStatus.OK - reposLinks?.size() == 3 + reposLinks?.size() == 4 when: "adding an entity" model.clear() diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/UuidTest.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/UuidTest.java new file mode 100644 index 000000000..d08512912 --- /dev/null +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/UuidTest.java @@ -0,0 +1,31 @@ +package org.springframework.data.rest.test.webmvc; + +import java.util.UUID; +import javax.persistence.Entity; +import javax.persistence.Id; + +/** + * @author Jon Brisbin + */ +@Entity +public class UuidTest { + + @Id UUID id = UUID.randomUUID(); + String name; + + public UuidTest() { + } + + public UUID getId() { + return id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/UuidTestRepository.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/UuidTestRepository.java new file mode 100644 index 000000000..8c5f0a4b3 --- /dev/null +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/UuidTestRepository.java @@ -0,0 +1,11 @@ +package org.springframework.data.rest.test.webmvc; + +import java.util.UUID; + +import org.springframework.data.repository.CrudRepository; + +/** + * @author Jon Brisbin + */ +public interface UuidTestRepository extends CrudRepository { +} diff --git a/spring-data-rest-webmvc/src/test/resources/META-INF/persistence.xml b/spring-data-rest-webmvc/src/test/resources/META-INF/persistence.xml index ed9d22292..87f693d94 100644 --- a/spring-data-rest-webmvc/src/test/resources/META-INF/persistence.xml +++ b/spring-data-rest-webmvc/src/test/resources/META-INF/persistence.xml @@ -4,6 +4,7 @@ org.springframework.data.rest.test.webmvc.Person org.springframework.data.rest.test.webmvc.Profile org.springframework.data.rest.test.webmvc.Address + org.springframework.data.rest.test.webmvc.UuidTest diff --git a/spring-data-rest-webmvc/src/test/resources/META-INF/spring-data-rest/repositories-export.xml b/spring-data-rest-webmvc/src/test/resources/META-INF/spring-data-rest/repositories-export.xml index c35c08655..82363476b 100644 --- a/spring-data-rest-webmvc/src/test/resources/META-INF/spring-data-rest/repositories-export.xml +++ b/spring-data-rest-webmvc/src/test/resources/META-INF/spring-data-rest/repositories-export.xml @@ -17,7 +17,6 @@ Uncomment this block to add the included UUID <-> String converters, which are not included by default. --> -