javaspring-webfluxspring-data-mongodb-reactive

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


  1. I'm new to MongoDB and WebFlux. I'm trying to retrieve a 'Post' with its 'Comments'(List listComments) using 'CommentsIds'(List idComments) from this 'Post' POJO:

  2. That's my 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. It is my Service (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. That's my controller:
    @GetMapping(FIND_POST_BY_ID_SHOW_COMMENTS)
    @ResponseStatus(OK)
    public Mono<Post> findPostByIdShowComments(@PathVariable String id) {
        return postService.findPostByIdShowComments(id);
    }
  1. As a result, I am not able to FEtch my 'Comments' and load them in the POJO, and consequently, I could not show them in the Rest Response Json. As a 'Json result' I am getting:
{
    "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"
    ]
}

Thanks a lot for your Guys help.


Solution

  • The reason why you don't receive comments is that your commentFlux and postFlux variables are just declared but never used in the execution chain.

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

    Now we injected comments flow and their injection into found post as a part of the main flow.