首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不映射扩展MongoRepository的存储库的REST端点

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

Stack Overflow用户
提问于 2019-05-15 15:11:05
回答 2查看 239关注 0票数 0

我已经实现了Spring Data Repositories,它使用@RepositoryRestResource注释扩展MongoRepository,以将它们标记为REST端点。但是当请求id被映射时,会得到以下异常

代码语言:javascript
复制
java.lang.IllegalArgumentException: Couldn't find PersistentEntity for type class io.sample.crm.models.Merchant!

存储库:

代码语言:javascript
复制
@RepositoryRestResource(collectionResourceRel = "account",path = "account")
public interface MerchantRepository extends MongoRepository<Merchant,String> { 

}

GET请求正在尝试:

代码语言:javascript
复制
http://localhost:9090/crm/account/

回应:

代码语言:javascript
复制
{
"cause": null,
"message": "Couldn't find PersistentEntity for type class io.apptizer.crm.apptizercrmservice.models.Merchant!"
}

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

Application.yml文件:

代码语言:javascript
复制
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

主类:

代码语言:javascript
复制
@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();
  }
}

这里会出什么问题呢?

EN

回答 2

Stack Overflow用户

发布于 2019-05-15 17:06:56

首先检查是否所有的依赖项都是正确的,added.The需要以下依赖项:

代码语言:javascript
复制
        <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无法识别域对象。尝试为商家提供实体类,如下所示:

代码语言:javascript
复制
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,将控制器添加到主应用程序中的组件扫描:

代码语言:javascript
复制
@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

票数 0
EN

Stack Overflow用户

发布于 2019-05-15 17:14:10

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56143533

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档