我一直在开发一个 Spring Boot Web 应用程序,我决定使用 ReactiveMongoRepository 来持久化数据。但是,当我保存文档时,我观察到一个异常,我在过去几天一直在努力解决它,但没有运气。例外是:
reactor.core.Exceptions$ErrorCallbackNotImplemented:
org.bson.codecs.configuration.CodecConfigurationException: Can't find a
codec for class com.mongodb.DBRef. Caused by:
org.bson.codecs.configuration.CodecConfigurationException: Can't find a
codec for class com.mongodb.DBRef.
at
org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46)
~[bson-3.4.2.jar:na]
请参考下面我的代码类和属性文件:
构建.gradle:
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-mongodb-
reactive')
compile('org.springframework.boot:spring-boot-starter-webflux')
compile('org.springframework.security:spring-security-core')
compile('org.springframework.security:spring-security-config')
compile('org.springframework.security:spring-security-webflux')
compileOnly('org.projectlombok:lombok')
compile("org.apache.tomcat.embed:tomcat-embed-jasper")
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('de.flapdoodle.embed:de.flapdoodle.embed.mongo')
testCompile('io.projectreactor:reactor-test')
}
应用程序属性
spring.data.mongodb.database=demo
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
应用程序.java
@SpringBootApplication
@EnableReactiveMongoRepositories
@EnableWebFluxSecurity
public class Application implements CommandLineRunner {
@Autowired
private PersonDetailsRespository personDetailsRespository;
@Autowired
private PersonRespository personRespository;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
private void dataBuilder() {
Person person = new Person();
person.setAge(23);
person.setName("Jitender");
personRespository.save(person).subscribe();
PersonDetails personDetails = new PersonDetails();
personDetails.setAddress("Address");
personDetails.setPerson(personRespository.findByName("Jitender").block());
personDetailsRespository.save(personDetails).subscribe();
}
@Override
public void run(String... args) throws Exception {
dataBuilder();
}
}
人.java
@Document
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Person {
public Person(String id) {
super();
this.id = id;
}
@Id
private String id;
private String name;
private int age;
}
PersonDetails.java
@Document
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class PersonDetails {
@Id private String id;
private String Address;
@DBRef
private Person person;
}
PersonRespository.java
public interface PersonRespository extends ReactiveMongoRepository<Person, String> {
Mono<Person> findByName(String name);
}
PersonDetailsRespository.java
public interface PersonDetailsRespository extends ReactiveMongoRepository<PersonDetails, String> {
}
在运行此应用程序时,会发生上述异常,我可以看到它使用的是 mongodb 驱动程序版本 mongodb-driver-core-3.4.2.jar。我注意到它在 PersonDetails.java 中的 Person 类上的 @DBref 有问题,如果我从 Application 中评论以下行,那么它可以工作
personDetails.setPerson(personRespository.findByName("Jitender").block());
提前致谢。吉丁