【问题标题】:REST endpoints of Repositories that extends MongoRepository do not get mapped未映射扩展 MongoRepository 的存储库的 REST 端点
【发布时间】:2019-10-02 06:07:06
【问题描述】:

我已经实现了 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();
  }
}

这里可能出了什么问题?

【问题讨论】:

  • 为什么你的网址里有crm? localhost:9090/crm/account/...can'tlocalhost:9090/account??
  • 希望这会有所帮助:stackoverflow.com/q/22824840/2987755,
  • @MSD 路径正确,因为base-path: /crm
  • 随便猜测一下,Merchant 集合中是否有任何数据,如果没有,请尝试添加一些虚拟对象并检查
  • 对不起,我认为基本 URL 未配置。所以我尝试使用 localhost:9090/account 。然后它给了我这个错误'"cause": null, "message": "Couldn't find PersistentEntity for type class io.apptizer.crm.apptizercrmservice.models.Merchant!"'

标签: java spring spring-data-rest mongorepository


【解决方案1】:

首先检查所有依赖是否正确添加。需要以下依赖:

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

请参考here

【讨论】:

  • 'spring-boot-starter-data-jpa' 这有必要吗?因为我在这里使用 MongoRepository
  • 抱歉,不需要。已经更新了 mongo repo 的答案
【解决方案2】:

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

请检查您的extends MongoRepository&lt;Merchant,String&gt; 是否与Merchant 的id 类型相同。

【讨论】:

  • 抱歉简单的发布,我无法在评论中写。 :(
  • 收到此回复。 ""message": "没有找到能够从 [org.bson.types.ObjectId] 类型转换为 [java.lang.Long] 类型的转换器","
  • 也就是说,任何地方的类型都是错误的。检查您的数据库列的类型。
  • 嗯,问题是我想将其保留为字符串类型 ID
  • 那么,在您最初的问题中,您是否继承并更改了 AbstractMongoConfiguration?
猜你喜欢
  • 1970-01-01
  • 2020-11-02
  • 2021-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多