我有一个带有spring-boot-starter-data-mongodb-reactive依赖项的springboot项目。我有以下代码用于将学生对象保存在集合中:
@Autowired
private ReactiveMongoTemplate reactiveMongoTemplate;
public Mono<Student> save(String studentDetails) throws IOException {
Student student = mapper.readValue(studentDetails, Student.class);
Mono<Student> studentMono = reactiveMongoTemplate.save(student, "StudentCollection");
return studentMono;
}
我已经配置了mongo客户端。上面的代码仅在我阻止流时才将数据保留在集合中。我该如何解决?
更新
我的mongo客户端具有如下所示的自定义配置:
public class MongoConfig {
@Autowired
private MongoSslContext mongoSslContext;
@Bean
public MongoClient mongoClient() throws UnrecoverableKeyException, KeyManagementException, KeyStoreException,
NoSuchAlgorithmException, CertificateException, IOException {
mycustomSslContext = mongoSslContext.createSSLContext();
SslSettings sslSettings = SslSettings.builder().context(mycustomSslContext).build();
MongoClientSettings.builder().sslSettings(sslSettings);
return MongoClients.create(new ConnectionString("mongodb://localhost:27019/studentDB?streamType=netty&ssl=true&authMechanism=MONGODB-X509"));
}
public ReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory() throws UnrecoverableKeyException,
KeyManagementException, KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
return new SimpleReactiveMongoDatabaseFactory(mongoClient(), "studentDB");
}
@Bean
public ReactiveMongoTemplate reactiveMongoTemplate() throws UnrecoverableKeyException, KeyManagementException,
KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
return new ReactiveMongoTemplate(reactiveMongoDatabaseFactory());
}
}
答案 0 :(得分:0)
使用FlatMap
,这就是使用反应式的方法。
public Mono<Student> save(String studentDetails) throws IOException {
Student student = mapper.readValue(studentDetails, Student.class);
Mono<Student> studentMono = reactiveMongoTemplate.save(student);
return studentMono;
}