Spring 被动 MongoDB 未保存文档

Spring Reactive MongoDB not saving document

我正在尝试保存基本文档,但尽管已成功连接到 mongodb...它似乎不想保存。

Spring 日志

2018-10-03 00:17:25.998  INFO 10713 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2018-10-03 00:17:26.049  INFO 10713 --- [  restartedMain] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-10-03 00:17:26.106  INFO 10713 --- [ctor-http-nio-1] r.ipc.netty.tcp.BlockingNettyContext     : Started HttpServer on /0:0:0:0:0:0:0:0:8080
2018-10-03 00:17:26.106  INFO 10713 --- [  restartedMain] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port(s): 8080
2018-10-03 00:17:26.112  INFO 10713 --- [  restartedMain] c.l.s.ServiceLegalApplicationKt          : Started ServiceLegalApplicationKt in 3.459 seconds (JVM running for 4.201)
2018-10-03 00:17:26.644  INFO 10713 --- [ntLoopGroup-2-2] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:3, serverValue:4}] to localhost:27017

application.properties

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=legal
spring.data.mongodb.repositories.type=reactive
spring.mongodb.embedded.version=4.0.2

基本界面和class

interface EventRepository: ReactiveMongoRepository<Event, String>

@Document
class Event(id: String, name: String)

尝试简单的保存功能

@Service
class SomeService(val eventRepository: EventRepository)
{
    fun save() = eventRepository.save(Event(UUID.randomUUID().toString(), "hey"))
}
Mono<Event> response = repository.save(Event(UUID.randomUUID().toString(), "hey"));

保存方式的变化

fun save() = eventRepository.save(Event(UUID.randomUUID().toString(), "hey")).subscribe();

您必须在 Mono 引用上调用 subscribe() 方法才能查看日志或详细信息。

为了让您使用 subscribe() 操作流式传输终端并同时获得单声道结果​​ - 分成两个单独的操作:

Mono<String> myEvent = eventRepository.save(Event(UUID.randomUUID().toString(), "hey"));
myEvent.subscribe();
return myEvent;