7

在这里,我在 mongodb 中配置了两个数据库。如本教程(链接)中所述。所以基本上我覆盖了MongoDataAutoConfigurationandMongoProperties实现。

属性 yml 文件:

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

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

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

MultipleMongoConfig 类:

@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 {
}

存储库类:

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

 }

用户模型类:

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

投影:

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

}

但是当我尝试向 REST 端点发出 GET 请求时,我在这里http://localhost:9090/users/遇到了java.lang.IllegalArgumentException: Couldn't find PersistentEntity for type class io.crm.service.models.User!异常。这里可能出了什么问题?想法将不胜感激。提前致谢。

4

0 回答 0