扩展MongoRepository的存储库的REST端点不会被映射

时间:2019-05-15 07:11:05

标签: java spring spring-data-rest mongorepository

我已经实现了Spring数据存储库,该存储库扩展了带有@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();
  }
}

这里可能出什么问题了?

2 个答案:

答案 0 :(得分:0)

最初检查所有依赖项是否正确添加。需要以下依赖项:

<intent-filter>
    <action android:name="android.intent.action.APPLICATION_PREFERENCES" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

错误响应显示ObjectAnimator flip = ObjectAnimator.ofFloat(viewToFlip, "rotationY", 0f, 360f); // or rotationX flip.setDuration(2000); // 2 seconds flip.start(); ,因此Merchant类可能不在类路径中,并且spring不能识别域对象。尝试为商家提供实体类,例如:

        <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!

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); } 告诉Spring在包中寻找其他组件,配置和服务,从而允许它找到控制器。

引用here

答案 1 :(得分:0)

遇到相同问题时,我将@Id更改为Long类型。

请检查您的extends MongoRepository<Merchant,String>Merchant的ID类型是否相同。