我正在使用DynamoDB构建Spring Boot应用程序。我想添加Spring Data REST。数据层有效,但REST在实体映射时失败。它正确地解析并创建了REST端点但我得到 PersistentEntity不能为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
当我调试代码时,我发现entities
中的PersistentEntityResourceAssembler
为空。这意味着我的实体没有注册。我想这是因为它们不是常规的JPA实体,只是通过存储库链接到数据层。
如何让Spring了解我的实体以使Data REST框架与DynamoDB协同工作?
以下是我项目的相关部分。虚拟实体表示发电机中的表:
@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添加到我想避免的类路径中,因为我不需要它。
答案 0 :(得分:-2)
我还开发了带有spring boot和dynamo db的应用程序,但我从未遇到过这个问题。在开发应用程序时我参考下面的示例