[英]How to convert Mono object to other Mono object in Spring Webflux
[英]How to make new mono with DTO from mono and flux in spring reactive webflux
在這里,我嘗試從數據庫中調用並從不同的 mono 和通量組合成新的 mono。
public Mono<ListMovieWithKomenDTO> fetchMovieAndKomen(Integer movieId){
Mono<Movie> movie = findById(movieId).subscribeOn(Schedulers.elastic());
Flux<MovieKomen> movieKomen = getKomenByMovieId(movieId).subscribeOn(Schedulers.elastic());
return Mono.zip(movie, movieKomen.collectList(), movieMovieKomenDTOBiFunction);
}
private BiFunction<Movie, List<MovieKomen>, ListMovieWithKomenDTO> movieMovieKomenDTOBiFunction = (x1, x2) -> ListMovieWithKomenDTO.builder()
// .age(x1.getAge())
.id(x1.getId())
.name(x1.getName())
.status(x1.getStatus())
.detail(x1.getDetail())
.url(x1.getUrl())
.movieKomen(x2).build();
在這里,我為 header (如電影)和詳細信息(如電影評論)兩次調用 db 以將它們分開。 在我檢索兩個不同的數據后,我想根據通量數據和 mono 加入新的 mono 數據。 為了使它們成為一個數據,我使 DTO 將電影表和評論表放在一起,但它失敗了。 我假設來自 mono.zip 的錯誤將數據輸入到一個新的 mono 中。
這是來自調試控制台的錯誤
java.lang.IllegalArgumentException: Cannot encode parameter of type org.springframework.r2dbc.core.Parameter
at io.r2dbc.postgresql.ExtendedQueryPostgresqlStatement.bind(ExtendedQueryPostgresqlStatement.java:89) ~[r2dbc-postgresql-0.8.10.RELEASE.jar:0.8.10.RELEASE]
謝謝
問題出在我使用的存儲庫中
public interface MovieKomenRepository extends ReactiveCrudRepository<MovieKomen,Integer> {
@Query("select * from m_movie_komen where m_movie_id = $1")
Flux<MovieKomen> findByMovieId(int movie_id);
}
在上面的示例中,我使用 $1 作為查詢中的參數。 但是當我像底部一樣更改我的代碼時。 它就像一個魅力。
public interface MovieKomenRepository extends ReactiveCrudRepository<MovieKomen,Integer> {
@Query("select * from m_movie_komen where m_movie_id = :movie")
Flux<MovieKomen> findByMovieId(@Param("movie") int movie_id);
}
因此,如果有人想使用我的服務代碼很好,但在存儲庫中要小心。 我們不應該使用 '$1' 而不是 ':movie'。 所以問題不在服務或單聲道/通量中。 但在我的存儲庫中
謝謝你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.