0

I have implemented Spring Data Repositories that extents MongoRepository with @RepositoryRestResource annotation to mark them as REST endpoints. But when the request id is mapped getting the following exception

java.lang.IllegalArgumentException: Couldn't find PersistentEntity for type class io.sample.crm.models.Merchant!

The Repository :

@RepositoryRestResource(collectionResourceRel = "account",path = "account")
public interface MerchantRepository extends MongoRepository<Merchant,String> { 

}

The GET request im trying :

http://localhost:9090/crm/account/

The response :

{
"cause": null,
"message": "Couldn't find PersistentEntity for type class io.apptizer.crm.apptizercrmservice.models.Merchant!"
}

Plus I have configured two databases for my each repository.

Application.yml file :

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

Main class :

@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();
  }
}

What could be gone wrong here?

GeekySelene
  • 847
  • 3
  • 13
  • 31
  • Why ur URL has crm in it? http://localhost:9090/crm/account/...can't it be http://localhost:9090/account/?? – MSD May 15 '19 at 07:29
  • hope this will help: https://stackoverflow.com/q/22824840/2987755, – dkb May 15 '19 at 07:30
  • @MSD path is correct since `base-path: /crm` – dkb May 15 '19 at 07:30
  • Just wild guess, is there any data in `Merchant` collection, if not try adding some dummy and check – dkb May 15 '19 at 07:31
  • sorry I think the base URL is not configured. So I tried with localhost:9090/account . Then it gave me this error '"cause": null, "message": "Couldn't find PersistentEntity for type class io.apptizer.crm.apptizercrmservice.models.Merchant!"' – GeekySelene May 15 '19 at 07:41
  • @dkb there are dummy data in the db document – GeekySelene May 15 '19 at 07:42
  • RepositoryRestResource does not exactly mark endpoint as REST, it customizes export mapping and rels. Start by looking at the start log of your application, if gives you the list of mapped urls. – Marc Tarin May 15 '19 at 09:01
  • @MarcTarin its being mapped without the base URL. But the issue is now getting a new exception. I updated the question – GeekySelene May 15 '19 at 09:03

2 Answers2

0

Initially check if all the dependencies are correctly added.The following dependencies are needed :

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

The error response shows that Couldn't find PersistentEntity for type class io.apptizer.crm.apptizercrmservice.models.Merchant! , so the Merchant class might not be in classpath and domain object is not getting identified by spring. Try providing an entity class for merchant like :

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

}

After that check to see if you have provided all the annotations properly.Try adding the controller to the component scan in the main application :

@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 tells Spring to look for other components, configurations, and services in the package, allowing it to find the controllers.

Refer here.

Ananthapadmanabhan
  • 5,706
  • 6
  • 22
  • 39
0

When i got same problem, i changed @Id to Long type.

Please check your extends MongoRepository<Merchant,String> is same with Merchant's type of id.

윤현구
  • 467
  • 7
  • 19