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.

This commit is contained in:
Jon Brisbin
2012-05-07 13:28:04 -05:00
parent e44506c4e0
commit 6c5bab7be3
7 changed files with 49 additions and 10 deletions

View File

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

View File

@@ -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<HttpMessageConverter<?>> httpMessageConverters = new ArrayList<HttpMessageConverter<?>>();
@@ -118,9 +114,6 @@ public class RepositoryRestMvcConfiguration {
.repositoryExporters(Arrays.<RepositoryExporter>asList(jpaRepositoryExporter()))
.httpMessageConverters(httpMessageConverters())
.jsonMediaType("application/json");
if (null != customConversionService) {
repositoryRestController.conversionService(customConversionService);
}
}
return repositoryRestController;
}

View File

@@ -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()

View File

@@ -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 <jbrisbin@vmware.com>
*/
@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;
}
}

View File

@@ -0,0 +1,11 @@
package org.springframework.data.rest.test.webmvc;
import java.util.UUID;
import org.springframework.data.repository.CrudRepository;
/**
* @author Jon Brisbin <jbrisbin@vmware.com>
*/
public interface UuidTestRepository extends CrudRepository<UuidTest, UUID> {
}

View File

@@ -4,6 +4,7 @@
<class>org.springframework.data.rest.test.webmvc.Person</class>
<class>org.springframework.data.rest.test.webmvc.Profile</class>
<class>org.springframework.data.rest.test.webmvc.Address</class>
<class>org.springframework.data.rest.test.webmvc.UuidTest</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.connection.url" value="jdbc:hsqldb:mem:spring"/>

View File

@@ -17,7 +17,6 @@
Uncomment this block to add the included UUID <-> String converters, which are not included by default.
-->
<!--
<bean class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
@@ -26,7 +25,6 @@
</set>
</property>
</bean>
-->
<!--
This validator will be picked up automatically. The default configuration is to look at the bean name