我已经实现了Spring Data Repositories,它使用@RepositoryRestResource注释扩展MongoRepository,以将它们标记为REST端点。但是当请求id被映射时,会得到以下异常
java.lang.IllegalArgumentException: Couldn't find PersistentEntity for type class io.sample.crm.models.Merchant!存储库:
@RepositoryRestResource(collectionResourceRel = "account",path = "account")
public interface MerchantRepository extends MongoRepository<Merchant,String> {
}GET请求正在尝试:
http://localhost:9090/crm/account/回应:
{
"cause": null,
"message": "Couldn't find PersistentEntity for type class io.apptizer.crm.apptizercrmservice.models.Merchant!"
}另外,我已经为我的每个存储库配置了两个数据库。
Application.yml文件:
spring:
autoconfigure:
exclude: org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration
mongodb:
primary:
host: 127.0.0.1
port: 27017
database: db_sample_admin_crm
rest:
base-path: /crm
secondary:
host: 127.0.0.1
port: 27017
database: sample_lead_forms
rest:
base-path: /reports主类:
@SpringBootApplication(scanBasePackages = "io.example")
@Configuration
@ComponentScan({"io.example"})
@EntityScan("io.example")
public class App {
public static void main(String[] args) throws Exception {
SpringApplication.run(App.class, args);
InitAuth.initialize();
InitAuth.generateToken();
}
}这里会出什么问题呢?
发布于 2019-05-15 17:06:56
首先检查是否所有的依赖项都是正确的,added.The需要以下依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>错误响应显示Couldn't find PersistentEntity for type class io.apptizer.crm.apptizercrmservice.models.Merchant!,因此Merchant类可能不在类路径中,并且spring无法识别域对象。尝试为商家提供实体类,如下所示:
public class Merchant{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
@RepositoryRestResource(collectionResourceRel = "account", path = "account")
public interface MerchantRepository extends MongoRepository<Merchant, String> {
List<Person> findByLastName(@Param("name") String name);
}之后,检查您是否提供了所有注释properly.Try,将控制器添加到主应用程序中的组件扫描:
@SpringBootApplication
@EnableMongoRepositories("com.example.MerchantRepository")
@ComponentScan(basePackages = {"com.example"})
@EntityScan("com.example.mongo.Merchant")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}@ComponentScan告诉Spring查找包中的其他组件、配置和服务,从而允许它找到控制器。
请参阅here。
发布于 2019-05-15 17:14:10
当我遇到同样的问题时,我将@Id改为Long类型。
请检查您的extends MongoRepository<Merchant,String>与Merchant的id类型是否相同。
https://stackoverflow.com/questions/56143533
复制相似问题