【问题标题】:"PersistentEntity must not be null" Exception with MongoDB and Spring Data RESTMongoDB 和 Spring Data REST 的“PersistentEntity 不能为空”异常
【发布时间】:2017-03-15 13:28:49
【问题描述】:

使用 Spring Data REST 和 MongoDB 创建项目的每一次尝试都遇到了同样烦人的问题。每个尝试访问 REST 端点的测试都会导致 java.lang.IllegalArgumentException: PersistentEntity must not be null!,由 PersistentEntityResource 构建器方法抛出。这意味着当启动应用程序上下文并初始化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());
  }

}

堆栈跟踪是:

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 平台版本 (Brussels-SR1) 的 Spring Boot、Spring Data MongoDB 和 Spring Data REST。使用 Spring Boot 运行应用程序,我没有收到任何错误,仅在测试时使用 SpringJUnit4ClassRunnerSpringRunner。我错过了什么?

【问题讨论】:

    标签: java mongodb junit spring-data spring-data-rest


    【解决方案1】:

    遇到同样的问题,不得不大量调试 spring 内部

    错误原因 - MongoTemplate 中缺少 MappingConverter 对象。 当 Spring 自动创建 mongoTemplate bean 时,使用以下构造函数

    public MongoTemplate(MongoDbFactory mongoDbFactory, MongoConverter mongoConverter) 
    

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

    1) 不要重新定义 MongoTemplate bean。您可以使用 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;
    }
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-26
      • 2016-08-20
      • 1970-01-01
      相关资源
      最近更新 更多