简体   繁体   English

如何使用 mono 中的 DTO 和 spring 反应性 webflux 中的焊剂制作新的 mono

[英]How to make new mono with DTO from mono and flux in spring reactive webflux

Here I try to make call from database and combine into new mono from different mono and flux.在这里,我尝试从数据库中调用并从不同的 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();

In here I make db call twice for header ( like movie ) and detail ( like movie comment ) to separate them.在这里,我为 header (如电影)和详细信息(如电影评论)两次调用 db 以将它们分开。 After I make retrieve two different data, I want to join into new mono data based on flux data and mono.在我检索两个不同的数据后,我想根据通量数据和 mono 加入新的 mono 数据。 to make them into one data, I make DTO to put together from movie table and comment table but it failed.为了使它们成为一个数据,我使 DTO 将电影表和评论表放在一起,但它失败了。 I assume that errors from mono.zip to get data into one new mono.我假设来自 mono.zip 的错误将数据输入到一个新的 mono 中。

Here the error from debug console这是来自调试控制台的错误

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]

Thank you谢谢

Problem is in my repository I used问题出在我使用的存储库中

public interface MovieKomenRepository  extends ReactiveCrudRepository<MovieKomen,Integer> {
    @Query("select * from m_movie_komen where m_movie_id = $1")
    Flux<MovieKomen> findByMovieId(int movie_id);
}

in above example, I used $1 for the param in query.在上面的示例中,我使用 $1 作为查询中的参数。 But when I change my code like bottom.但是当我像底部一样更改我的代码时。 It works like a charm.它就像一个魅力。

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);
}

so if someone want to use my service code is fine but careful in repository.因此,如果有人想使用我的服务代码很好,但在存储库中要小心。 we should not used '$1' instead ':movie'.我们不应该使用 '$1' 而不是 ':movie'。 so the problem not in service or mono/flux.所以问题不在服务或单声道/通量中。 but in my repository但在我的存储库中

Thank you.谢谢你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM