Spring boot 保存文档时Spring boot Mongo反应存储库问题。错误:[无法找到com.mongodb.DBRef类的编解码器]
我一直在开发一个SpringBootWeb应用程序,我决定使用ReactiveMongorPository来持久化数据。但当我保存文档时,我发现了一个异常,我在过去几天里一直在努力解决它,但运气不好。例外情况是:Spring boot 保存文档时Spring boot Mongo反应存储库问题。错误:[无法找到com.mongodb.DBRef类的编解码器],spring-boot,functional-programming,spring-data-mongodb,reactive-streams,Spring Boot,Functional Programming,Spring Data Mongodb,Reactive Streams,我一直在开发一个SpringBootWeb应用程序,我决定使用ReactiveMongorPository来持久化数据。但当我保存文档时,我发现了一个异常,我在过去几天里一直在努力解决它,但运气不好。例外情况是: reactor.core.Exceptions$ErrorCallbackNotImplemented: org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class
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')
}
应用程序属性
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);
}
public interface PersonDetailsRespository extends ReactiveMongoRepository<PersonDetails, String> {
}
提前谢谢。
Jitender反应式API不支持DBRefs。引用迟早需要解决,没有API可以解决。朋友们,我已经解决了这个问题,这个问题是在我使用的spring boot版本上出现的,这个问题发生在springBootVersion='2.0.0.M2'上,就像我使用版本M6一样,它得到了纠正,例如springBootVersion='2.0.0.M6'。感谢被动API不支持DBREF。引用迟早需要解决,没有API可以解决。朋友们,我已经解决了这个问题,这个问题是在我使用的spring boot版本上出现的,这个问题发生在springBootVersion='2.0.0.M2'上,就像我使用版本M6一样,它得到了纠正,例如springBootVersion='2.0.0.M6'。谢谢
personDetails.setPerson(personRespository.findByName("Jitender").block());