0

我已经实现了 Spring Data Repositories,它使用注释扩展 MongoRepository@RepositoryRestResource以将它们标记为 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!"
}

另外,我为每个存储库配置了两个数据库。

应用程序.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();
  }
}

这里可能出了什么问题?

4

2 回答 2

0

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

        <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);

}

之后检查您是否已正确提供所有注释。尝试将控制器添加到主应用程序中的组件扫描:

@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 在包中查找其他组件、配置和服务,使其能够找到控制器。

参考这里

于 2019-05-15T09:06:56.460 回答
0

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

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

于 2019-05-15T09:14:09.740 回答