2017-03-15 30 views
3

每一次使用Spring Data REST和MongoDB創建項目的嘗試都遇到了同樣令人討厭的問題。每個嘗試訪問REST端點的測試都會導致java.lang.IllegalArgumentException: PersistentEntity must not be null!,由PersistentEntityResource構建器方法拋出。這意味着當應用程序上下文啓動並且RepositoryRestMvcConfiguration被初始化時,那個bean是空的。一些示例代碼:「PersistentEntity不能爲空」MongoDB和Spring Data REST的例外

@Document 
public class Person { 
    @Id private String id; 
    private String name; 
    private Integer age; 
    // Getters and setters 
} 

@RepositoryRestResource(path = "persons", collectionResourceRel = "persons") 
public interface PersonRepository extends MongoRepository<Person, String> { 
} 

@Configuration 
@EnableMongoRepositories(basePackages = { "me.woemler.test" }) 
public class DataSourceConfig { 

    @Bean(destroyMethod = "close") 
    public Mongo mongo() throws IOException { 
    return new EmbeddedMongoBuilder().build(); 
    } 

    @Bean 
    public MongoTemplate mongoTemplate(Mongo mongo){ 
    return new MongoTemplate(mongo, "test-db"); 
    } 

} 

@RunWith(SpringJUnit4ClassRunner.class) 
@WebAppConfiguration 
@ContextConfiguration(classes = {DataSourceConfig.class, RepositoryRestMvcConfiguration.class}) 
public class PersonTests { 

    @Autowired private PersonRepository personRepository; 
    @Autowired private WebApplicationContext context; 
    private MockMvc mockMvc; 

    @Before 
    public void setup(){ 
    personRepository.deleteAll(); 
    Person person = new Person(); 
    person.setName("Joe"); 
    person.setAge(33); 
    personRepository.save(person); 
    mockMvc = MockMvcBuilders.webAppContextSetup(context).build(); 
    } 

    @Test 
    public void test() throws Exception{ 
     mockMvc.perform(MockMvcRequestBuilders.get("/persons")) 
     .andExpect(MockMvcResultMatchers.status().isOk()); 
    } 

} 

堆棧跟蹤是:

Caused by: java.lang.IllegalArgumentException: PersistentEntity must not be null! 
    at org.springframework.util.Assert.notNull(Assert.java:134) 
    at org.springframework.data.rest.webmvc.PersistentEntityResource$Builder.<init>(PersistentEntityResource.java:140) 
    at org.springframework.data.rest.webmvc.PersistentEntityResource$Builder.<init>(PersistentEntityResource.java:123) 
    at org.springframework.data.rest.webmvc.PersistentEntityResource.build(PersistentEntityResource.java:115) 
    at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.wrap(PersistentEntityResourceAssembler.java:74) 
    at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.toResource(PersistentEntityResourceAssembler.java:55) 
    at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.toResource(PersistentEntityResourceAssembler.java:38) 
    at org.springframework.data.web.PagedResourcesAssembler.createResource(PagedResourcesAssembler.java:200) 
    at org.springframework.data.web.PagedResourcesAssembler.toResource(PagedResourcesAssembler.java:132) 
    at org.springframework.data.rest.webmvc.AbstractRepositoryRestController.entitiesToResources(AbstractRepositoryRestController.java:92) 
    at org.springframework.data.rest.webmvc.AbstractRepositoryRestController.toResources(AbstractRepositoryRestController.java:76) 
    at org.springframework.data.rest.webmvc.RepositoryEntityController.getCollectionResource(RepositoryEntityController.java:209) 

我使用Spring啓動,春數據的MongoDB和Spring數據REST與最新的Spring平臺版本(布魯塞爾-SR1)。使用Spring Boot運行應用程序,我不會收到任何錯誤,只有在使用SpringJUnit4ClassRunnerSpringRunner進行測試時纔會發生。我錯過了什麼?

回答

3

當初同樣的問題,必須調試春季內部很多

錯誤的原因 - 缺少MappingConverter對象MongoTemplate。 當mongoTemplate bean被自動彈簧下面的構造函數創建用於

public MongoTemplate(MongoDbFactory mongoDbFactory, MongoConverter mongoConverter) 

爲了解決這個問題,有兩種選擇:

1)不要重新定義MongoTemplate豆。您可以使用application.properties指定數據庫

spring.data.mongodb.uri=mongodb://hostname:27017/dbName 

2)自動裝配mongoConverter和創作mongoTemplate

@Autowired 
private MongoConverter mongoConverter; 

public @Bean 
MongoTemplate mongoTemplate() throws Exception { 
    MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory(), mongoConverter); 
    return mongoTemplate; 
} 

希望用這有助於

相關問題