java webclient使用,java – Spring Reactive WebClient

我有一个反应式休息api(webflux),也使用

spring WebClient类,从其他休息服务请求数据.

简化设计:

@PostMapping(value = "/document")

public Mono save(@RequestBody Mono document){

//1st Problem: I do not know how to get the documentoOwner ID

//that is inside the Document class from the request body without using .block()

Mono documentOwner = documentOwnerWebClient()

.get().uri("/document-owner/{id}", document.getDocumentOwner().getId())

.accept(MediaType.APPLICATION_STREAM_JSON)

.exchange()

.flatMap(do -> do.bodyToMono(DocumentOwner.class));

//2nd Problem: I need to check (validate) if the documentOwner object is "active", for instance

//documentOwner and document instances below should be the object per se, not the Mono returned from the external API

if (!documentOwner.isActive) throw SomeBusinessException();

document.setDocumentOwner(documentOwner);

//Now I can save the document in some reactive repository,

//and return the one saved with the given ID.

return documentRepository.save(document)

}

换句话说:我理解(几乎)单独的所有反应性示例,但我无法将它们全部放在一起并构建一个简单的用例(get – > validate – > save – > return)而不会阻塞对象.

我越接近的是:

@PostMapping(value = "/document")

public Mono salvar(@RequestBody Mono documentRequest){

return documentRequest

.transform(this::getDocumentOwner)

.transform(this::validateDocumentOwner)

.and(documentRequest, this::setDocumentOwner)

.transform(this::saveDocument);

}

辅助方法有:

private Mono getDocumentOwner(Mono document) {

return document.flatMap(p -> documentOwnerConsumer.getDocumentOwner(p.getDocumentOwnerId()));

}

private Mono validateDocumentOwner(Mono documentOwner) {

return documentOwner.flatMap(do -> {

if (do.getActive()) {

return Mono.error(new BusinessException("Document Owner is Inactive"));

}

return Mono.just(do);

});

}

private DocumentOwnersetDocumentOwner(DocumentOwner documentOwner, Document document) {

document.setDocumentOwner(documentOwner);

return document;

}

private Mono saveDocument(Mono documentMono) {

return documentMono.flatMap(documentRepository::save);

}

我正在使用Netty,SpringBoot,Spring WebFlux和Reactive Mongo Repository.但是有一些问题:

1)我收到错误:

java.lang.IllegalStateException:只允许一个连接接收订阅者.也许是因为我使用相同的documentRequest转换和setDocumentOwner.我真的不知道.

2)没有调用setDocumentOwner方法.因此,不保存要保存的文档对象.我相信可以有更好的方法来实现这个setDocumentOwner().

谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值