我一直在研究一个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]
请参阅下面的我的代码类和属性文件:
的build.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')
}
application.properties
spring.data.mongodb.database=demo
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
Application.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();
}
}
Person.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());
提前致谢。 Jitender