[英]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.