【发布时间】:2017-01-29 01:05:57
【问题描述】:
我正在使用 DynamoDB 构建 Spring Boot 应用程序。我想添加 Spring Data REST。数据层工作,但 REST 在实体映射上失败。它正确解析并创建了 REST 端点,但我收到 PersistentEntity must not be null! 消息,并且在访问 REST API 时出现异常:
java.lang.IllegalArgumentException: PersistentEntity must not be null!
at org.springframework.util.Assert.notNull(Assert.java:115)
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.AbstractRepositoryRestController.entitiesToResources(AbstractRepositoryRestController.java:133)
at org.springframework.data.rest.webmvc.AbstractRepositoryRestController.toResources(AbstractRepositoryRestController.java:80)
at org.springframework.data.rest.webmvc.RepositoryEntityController.getCollectionResource(RepositoryEntityController.java:212)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
removed irrelevant parts of the exception
当我调试代码时,我看到PersistentEntityResourceAssembler 中的entities 是空的。这意味着我的实体没有注册。我猜这是因为它们不是常规的 JPA 实体,它们仅通过存储库链接到数据层。
如何让 Spring 了解我的实体,以使 Data REST 框架与 DynamoDB 一起使用?
以下是我项目中的相关部分。 dynamo 中表示表格的虚拟实体:
@DynamoDBTable(tableName = "DummyTable")
public class Tester {
@Id
private String id;
@DynamoDBHashKey(attributeName = "id")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
存储库:
public interface TesterRepository extends CrudRepository<Tester, String> {
@EnableScan
@Override
Iterable<Tester> findAll();
}
配置类:
@Configuration
@EnableDynamoDBRepositories(basePackages = "com.czequered.promocodes.repository")
public class DynamoDBConfig {
// deleted params
@Bean
public AmazonDynamoDB amazonDynamoDB() {
// deleted the simple initializer, just long code
return amazonDB;
}
}
和 gradle 依赖关系:
compile 'com.amazonaws:aws-java-sdk-core:1.11.86'
compile 'org.springframework.boot:spring-boot-starter'
compile 'org.springframework.boot:spring-boot-starter-data-rest'
compile 'com.github.derjust:spring-data-dynamodb:4.4.1'
代码已经简化,其余的可以在这个gist with all files needed to build the whole application找到。
I tried to follow the demo from the original author of the Spring Data DynamoDB library,但它会将 JPA 和 Hibernate 添加到我想要避免的类路径中,因为我不需要它们。
【问题讨论】:
-
我也有同样的问题。你找到解决办法了吗?
-
@EricZhang 我最终没有使用 DynamoDB 的 spring 库。你可以看到我在这个项目中做了什么:github.com/sm4/promo-codes
标签: java spring spring-boot amazon-dynamodb spring-data-rest