'Couldn't find PersistentEntity for type class' exception in Spring boot MongoRepository

对着背影说爱祢 提交于 2019-12-12 12:30:28

问题


In here I have configured two databases in mongodb. As described in this tutorial (link). So basically I override the MongoDataAutoConfiguration and MongoProperties implementations.

The property yml file :

spring:
  autoconfigure:
       exclude: 
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration

mongodb:
   primary:
     host: 127.0.0.1
     port: 27017
     database: db_admin_crm
   secondary:
     host: 127.0.0.1
     port: 27017
     database: lead_forms

MultipleMongoProperties class :

@Data
@ConfigurationProperties(prefix = "mongodb")
public class MultipleMongoProperties {

 private MongoProperties primary = new MongoProperties();
 private MongoProperties secondary = new MongoProperties();
    //getters and setters
}

MultipleMongoConfig class :

@Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties(MultipleMongoProperties.class)
public class MultipleMongoConfig {

@Autowired
private final MultipleMongoProperties mongoProperties;

public MultipleMongoConfig() {
    mongoProperties = null;
}


@Primary
@Bean(name = "primaryMongoTemplate")
public MongoTemplate primaryMongoTemplate() throws Exception {
    return new MongoTemplate(primaryFactory(this.mongoProperties.getPrimary()));
}

@Bean(name = "secondaryMongoTemplate")
public MongoTemplate secondaryMongoTemplate() throws Exception {
    return new MongoTemplate(secondaryFactory(this.mongoProperties.getSecondary()));
}

@Bean
@Primary
public MongoDbFactory primaryFactory(final MongoProperties mongo) throws Exception {
    return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()),
            mongo.getDatabase());
}

@Bean
public MongoDbFactory secondaryFactory(final MongoProperties mongo) throws Exception {
    return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()),
            mongo.getDatabase());
  }
}

PrimaryMongoConfig :

@Configuration
@EnableMongoRepositories(basePackages = "io.crm.service.repositories",
    mongoTemplateRef = "primaryMongoTemplate")
public class PrimaryMongoConfig{
}

SecondaryMongoConfig :

@Configuration
@EnableMongoRepositories(basePackages = "io.crm.service.repositories.report.repositories",
    mongoTemplateRef = "secondaryMongoTemplate")
public class SecondaryMongoConfig {
}

The Repository class :

   @RepositoryRestResource(collectionResourceRel = "users",path = "users",excerptProjection = UserProjection.class)
   public interface UserRepository extends MongoRepository<User, String> {

 }

User model class :

@Id
private String id;
private String email;
private String name;
private String businessName;
private String phone;
private String address;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private Date createdTime;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private Date updatedTime;
@Field("bookletSignUps")
@DBRef
private List<BookletSignUp> bookletSignUps;
@Field("eventSignUps")
@DBRef
private List<EventSignUp> eventSignUps;
@Field("infoSignUps")
@DBRef
private List<InfoSignUp> infoSignUps;
@Field("webinarSignUps")
@DBRef
private List<WebinarSignUp> webinarSignUps;

The projection :

@Projection(name = "userExcerpt", types = User.class)
public interface UserProjection {

String getId();
String getName();
String getEmail();
String getBusinessName();
String getPhone();
String getAddress();
Date getCreatedTime();
Date getUpdatedTime();
List<BookletSignUp> getBookletSignUps();
List<EventSignUp> getEventSignUps();
List<InfoSignUp> getInfoSignUps();
List<WebinarSignUp> getWebinarSignUps();

}

But when im trying to do a GET request to the REST endpoint http://localhost:9090/users/ im getting java.lang.IllegalArgumentException: Couldn't find PersistentEntity for type class io.crm.service.models.User! exception here. What could be gone wrong here? Ideas will be very much appreciated. Thanks in advance.

来源:https://stackoverflow.com/questions/56041911/couldnt-find-persistententity-for-type-class-exception-in-spring-boot-mongore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!