我试图保留调度程序生成的某些日期,然后以流方式通过rest终结点读取它们。我有一个非常简单的模型,一个Feed
仅包含uuid和一些文本。我还有一个扩展ReactiveCouchbaseRepository<Feed, String>
的存储库。
当我使用CouchbaseRepository<Feed, String>
时,持久保持有效。
这是我的Feed
:
@Document
public class Feed {
@Id
private String id;
@Field
private String text;
...
和存储库:
@N1qlPrimaryIndexed
@ViewIndexed(designDoc = "feed")
public interface FeedRepository extends ReactiveCouchbaseRepository<Feed, String> {
}
以及调度程序:
....
@Autowired
private FeedRepository feedRepository;
@Scheduled(fixedRate = 1000)
public void generate(){
feedRepository.save(new Feed(UUID.randomUUID().toString(), String.valueOf(System.currentTimeMillis())));
...
这是我的终点:
@RestController
public class MainController {
@Autowired
private FeedRepository feedRepository;
@GetMapping(value = "/get/feed/live", produces = "text/event-stream")
public Flux<Feed> getLiveFeeds(){
return feedRepository.findAll();
}
}
当我呼叫端点时,我得到了:
Tue Apr 09 15:12:02 CEST 2019
There was an unexpected error (type=Internal Server Error, status=500).
View feed/all does not exist.
com.couchbase.client.java.error.ViewDoesNotExistException: View feed/all does not exist.
at com.couchbase.client.java.view.ViewQueryResponseMapper$BuildViewResult.call(ViewQueryResponseMapper.java:211)
at com.couchbase.client.java.view.ViewQueryResponseMapper$BuildViewResult.call(ViewQueryResponseMapper.java:185)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(OnSubscribeMap.java:69)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(OnSubscribeMap.java:77)
at rx.internal.producers.SingleProducer.request(SingleProducer.java:65)
at rx.Subscriber.setProducer(Subscriber.java:211)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.setProducer(OnSubscribeMap.java:102)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.setProducer(OnSubscribeMap.java:102)
at rx.internal.operators.OperatorSingle$ParentSubscriber.onCompleted(OperatorSingle.java:113)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.checkTerminated(OperatorObserveOn.java:281)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:216)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Assembly trace from producer [reactor.core.publisher.FluxDefer] :
reactor.core.publisher.Flux.defer(Flux.java:805)
org.springframework.data.repository.util.ReactiveWrapperConverters$RxJava1ObservableToFluxConverter.convert(ReactiveWrapperConverters.java:682)
Error has been observed by the following operator(s):
|_ Flux.defer ⇢ org.springframework.data.repository.util.ReactiveWrapperConverters$RxJava1ObservableToFluxConverter.convert(ReactiveWrapperConverters.java:682)
|_ Flux.map ⇢ org.springframework.http.codec.ServerSentEventHttpMessageWriter.encode(ServerSentEventHttpMessageWriter.java:119)
|_ Mono.doOnError ⇢ org.springframework.http.server.reactive.AbstractServerHttpResponse.writeAndFlushWith(AbstractServerHttpResponse.java:186)
|_ Mono.flatMap ⇢ org.springframework.web.reactive.DispatcherHandler.lambda$handleResult$5(DispatcherHandler.java:175)
|_ Mono.onErrorResume ⇢ org.springframework.web.reactive.DispatcherHandler.handleResult(DispatcherHandler.java:175)
|_ Mono.flatMap ⇢ org.springframework.web.reactive.DispatcherHandler.handle(DispatcherHandler.java:152)
|_ Mono.defer ⇢ org.springframework.web.server.handler.DefaultWebFilterChain.filter(DefaultWebFilterChain.java:119)
|_ Mono.doAfterSuccessOrError ⇢ org.springframework.boot.actuate.web.trace.reactive.HttpTraceWebFilter.filter(HttpTraceWebFilter.java:99)
|_ Mono.flatMap ⇢ org.springframework.boot.actuate.web.trace.reactive.HttpTraceWebFilter.filter(HttpTraceWebFilter.java:82)
|_ Mono.defer ⇢ org.springframework.web.server.handler.DefaultWebFilterChain.filter(DefaultWebFilterChain.java:119)
|_ Mono.defer ⇢ org.springframework.web.server.handler.DefaultWebFilterChain.filter(DefaultWebFilterChain.java:119)
|_ Mono.doOnSuccess ⇢ org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter.filter(MetricsWebFilter.java:84)
|_ Mono.doOnError ⇢ org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter.filter(MetricsWebFilter.java:84)
|_ Mono.compose ⇢ org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter.filter(MetricsWebFilter.java:76)
|_ Mono.defer ⇢ org.springframework.web.server.handler.DefaultWebFilterChain.filter(DefaultWebFilterChain.java:119)
Caused by: rx.exceptions.OnErrorThrowable$OnNextValue: OnError while emitting onNext value: com.couchbase.client.java.document.json.JsonObject.class
at rx.exceptions.OnErrorThrowable.addValueAsLastCause(OnErrorThrowable.java:118)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(OnSubscribeMap.java:73)
... 16 more
谢谢您的帮助。
编辑1: 为了坚持下去,有三件事是错误的/缺失的:
@EnableReactiveCouchbaseRepositories
丢失(在答案之一中指出)AbstractReactiveCouchbaseConfiguration
,而是常规扩展feedRepository.save(new Feed(UUID.randomUUID().toString(), String.valueOf(System.currentTimeMillis())))
在最后遗失.subscribe()
。 现在,持久部分起作用了。 提取仍有问题。
编辑2
刚刚在调用端点时在日志中找到此调试消息:
2019-04-10 16:27:06.467 DEBUG 11172 --- [-computations-3] c.c.client.java.view.ViewRetryHandler : Design document not found, error is {"errors":[{"error":"not_found","reason":"Design document _design/feed not found"}
答案 0 :(得分:2)
您好像忘记了使用@EnableReactiveCouchbaseRepositories批注启用反应性存储库:
@SpringBootApplication
@EnableScheduling
@EnableReactiveCouchbaseRepositories
public class ReactiveDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ReactiveDemoApplication.class, args);
}
}
您可以在此处了解更多信息:
答案 1 :(得分:1)
看起来您的FeedRepository界面缺少用于执行搜索的视图的名称。
请尝试使用以下视图名称:
@N1qlPrimaryIndexed
@ViewIndexed(designDoc = "feed", viewName = "all")
public interface FeedRepository extends ReactiveCouchbaseRepository<Feed, String> {
}
对我来说,添加视图名称后它就可以工作。
答案 2 :(得分:0)
有点晚了,但希望这可以对其他人有所帮助。
在反应式编程中,您需要订阅发布者,否则,信息流将不会开始。在您的示例中,feedRepository.save(***)
从未执行,因为您需要类似feedRepository.save(**).subscribe()
的东西。
对于第二个问题,当您请求所有商品时,需要指定诸如{item}/all
之类的视图,对于较新的Couchbase版本,您可以指定PrimaryKey作为获取所有商品的必需索引。