"PersistentEntity must not be null" MongoDB和Spring Data REST的异常



每次尝试使用Spring Data Rest创建项目,MongoDB遇到了同一令人讨厌的问题。每次尝试访问REST端点的测试都会导致java.lang.IllegalArgumentException: PersistentEntity must not be null!,并由PersistentEntityResource Builder方法抛出。这意味着当启动应用程序上下文并初始化RepositoryRestMvcConfiguration时,PersistentEntities BEAN是空的。一些示例代码:

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

stacktrace是:

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 Boot,Spring Data MongoDB和Spring Data与最新的Spring Platform版本(Brussels-SR1)休息。使用Spring Boot运行该应用程序,我不会收到任何错误,仅在测试时,同时使用SpringJUnit4ClassRunnerSpringRunner。我想念什么?

也有同样的问题,不得不调试弹簧内部词

错误的原因 - 蒙古特板中缺少映射converter对象。当使用Spring sufter sustructor创建Mongotemplate Bean

public MongoTemplate(MongoDbFactory mongoDbFactory, MongoConverter mongoConverter) 

要解决此问题,有两个选择:

1)不要重新定义蒙古特板豆。您可以使用application.properties指定数据库

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

2)Autowire Mongoconverter,并用于创建Mongotemplate

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

希望这有帮助

最新更新