简体   繁体   中英

Call async task in Flux/Mono (Spring boot webflux)

I have 2 entities User, UserDetails and 2 repositories:

@Getter
@Setter
@NoArgsConstructor
class User {
    @Id
    private Long id;
    private String fullname;
}

@Getter
@Setter
@NoArgsConstructor
class UserDetails {
    @Id
    private Long id;
    private Long userId;
    @Transient
    private String fullname;
    private String hobby
    // and some more ...
}

@Repository
public interface UserRepo extends ReactiveCrudRepository<User, Long>;

@Repository
public interface UserDetailsRepo extends ReactiveCrudRepository<UserDetails, Long>;

I wish to get UserDetails list with fullname from User entity, so I coded in Route Function:

userDetailsRepo.findAll()
    .map(userDetails -> {
        userRepo.findById((userDetails.getUserId())
            .subscribe(user -> userDetails.setFullname((user.getFullname))
        return userDetails;
    }

But fullname is allways null.

Can everyone help me

Thanks so much

Sorry my english too bad

Thanks to JB Nizet and your advice. I edit the code and it works

userDetailsRepo.findAll()
    .flatMap(userDetails -> {
        return userRepo.findById(userDetails.getUserId())
        .map(user -> {
            userDetails.setFullname(user.getFullname());
            return userDetails;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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