【发布时间】:2020-10-05 12:25:00
【问题描述】:
我用的是Spring Webflux,需要从Mono<Book>返回Author的对象,但是不知道怎么做更正确。
应该返回Author的方法:
private Author getRegistredAuthor(Book bookInput) {
String fullName = bookInput.getAuthor().getFullName();
Optional<Author> optionalAuthor = repository.findByFullName(fullName).blockOptional();
if(optionalAuthor.isPresent()){
return optionalAuthor.get();
}else {
Author author = Author.builder().fullName(fullName).build();
return repository.insert(author).block();
}
}
使用Author创建新实体的方法:
public Mono<BookDto> create(@RequestBody Book book) {
book.setAuthor(getRegistredAuthor(book));
return bookRepository.insert(book)
.map(BookDto::of);
}
据我了解,如果我使用block(),我会大大降低reactive的有效性。
这样用会不会更有效?
return repository.insert(author).blockOptional().get();
如何返回Author 的对象最有效?
【问题讨论】:
-
返回
Mono<Author>,完全不要屏蔽。 -
@123 我需要
Author,因为这个方法是用来创建其他实体的 -
那么其他实体需要由 Mono 制作。要么一切都是被动的,要么毫无意义。
-
@123 但我不明白该怎么做。在其他地方我使用了
.map (BookDto :: of),但如果我不需要dto,我不知道如何应用它,而是Book或Author本身 -
只是不要映射它......
标签: spring-boot reactive-programming spring-webflux