如何在 Reactive Spring 数据中使用来自其他对象的引用获取对象?

How to Fetch objects, using references from other objects, in Reactive Spring Data?

  1. 我是 MongoDB 和 WebFlux 的新手。我正在尝试使用 'CommentsIds'(List idComments) 从这个 'Post' POJO:

    中检索一个 'Post' 及其 'Comments'(List listComments)
  2. 那是我的 POJO/Entity:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = "post")
public class Post implements Serializable {

    private static final long serialVersionUID = -6281811500337260230L;

    @EqualsAndHashCode.Include
    @Id
    private String id;
    private Date date;
    private String title;
    private String body;
    private AuthorDto author;
    private Comment comment;
    private List<Comment> listComments = new ArrayList<>();
    private List<String> idComments = new ArrayList<>();
}
  1. 这是我的服务 (findPostByIdShowComments + findCommentsByPostId):
    public Mono<Post> findPostByIdShowComments(String id) {
        return postRepo
                .findById(id)
                .switchIfEmpty(postNotFoundException())
                .flatMap(postFound -> {

                    Flux<Comment> commentFlux = commentService
                            .findCommentsByPostId(postFound.getId());

                    Flux<Post> postFlux = commentFlux
                            .flatMap(comment -> {
                                postFound.setListComments(new ArrayList<>());
                                postFound.getListComments()
                                         .add(comment);
                                return Mono.just(postFound);
                            });

                    return Mono.just(postFound);
                });
    }


    public Flux<Comment> findCommentsByPostId(String id) {
        return postRepo
                .findById(id)
                .switchIfEmpty(postNotFoundException())
                .thenMany(commentRepo.findAll())
                .filter(comment1 -> comment1.getIdPost()
                                            .equals(id));

    }
  1. 那是我的控制器:
    @GetMapping(FIND_POST_BY_ID_SHOW_COMMENTS)
    @ResponseStatus(OK)
    public Mono<Post> findPostByIdShowComments(@PathVariable String id) {
        return postService.findPostByIdShowComments(id);
    }
  1. 因此,我无法提取我的 'Comments' 并将它们加载到 POJO 中,因此,我无法在 Rest Response Json 中显示它们。 作为 'Json result' 我得到:
{
    "id": "5ff0ed93b3268a39dd3bd7b9",
    "date": "2011-11-11T00:00:00.000+00:00",
    "title": "Lake Cliffordside",
    "body": "North Sanfordborough",
    "author": {
        "id": "5ff0ed93b3268a39dd3bd7b6",
        "name": "melissa"
    },
    "comment": null,
    "listComments": [], //TROUBLE: As you can see, The CommentObjects are not coming
    "idComments": [
        "5ff0edb2b3268a39dd3bd7bc",
        "5ff0edbab3268a39dd3bd7bd"
    ]
}

非常感谢你们的帮助。

您没有收到评论的原因是您的 commentFluxpostFlux 变量只是声明但从未在执行链中使用。

public Mono<Post> findPostByIdShowComments(String id) {
    return postRepo
            .findById(id)
            .switchIfEmpty(postNotFoundException())
            .flatMap(postFound -> commentService
                    .findCommentsByPostId(postFound.getId())
                    .collectList()
                    // it might be also a good option (and a cleaner operator here) instead of .flatMap(...)
                    //.doOnSuccess(comments -> postFound.setListComments(comments))
                    //.thenReturn(postFound)
                    .flatMap(comments -> {
                        postFound.setListComments(comments);
                        return Mono.just(postFound);
                    })
            );
}

现在我们将评论流及其注入发现 post 作为主要流程的一部分。