简体   繁体   中英

webflux how to pass current login Mono string to a flux

I am still new to Spring boot Webflux. I am using Casssandra as my database, below i have a friends tables, i want to get all my friends based on my login ID. How can i get the current login ID and pass the ID to a flux

// Here i retrieved currently login user ID which is a mono string

public Mono<String> getUserID(Mono<String> principal) {
  return principal
   .map(Principal::getName)
}

OR

public static Mono<String> getUserIDFromRequest(ServerRequest request) {
    return request.principal()
            .cast(UsernamePasswordAuthenticationToken.class)
            .map(UsernamePasswordAuthenticationToken::getName);
}

// My friends table

public class Friends {
  @PrimaryKey("userid")
  private long userId;
  private long friendId;
  private FriendObject friendObject;
  private String since;
  private boolean enableNotifications;
  private boolean allowMessages;
  // Getters and setters
}

@Repository
public interface FriendsRepository extends ReactiveCassandraRepository<Friends, Long> {
}

@Service
public class FriendshipServiceImpl implements FriendshipService {
  @Override
  public Mono<Friends> addFriend(Friends friends) {
    return friendsRepository.save(friends);
  }
}

public class FriendshipHandler {

  // How to pass the Login Mono<String> to this flux Or how can i combine Mono and Flux?
  @GetMapping(path="/friends/list", produces=MediaType.TEXT_EVENT_STREAM_VALUE)
  Flux<Friends> getFriends(Mono<Principal> principal) {
    return friendshipService.getFriends(Long.valueOf("The login ID"));
  }

  OR

  public Mono<ServerResponse> getFriends(ServerRequest request) {
     return ok().body(
      friendshipService
        .getFriends(Long.valueOf("The login ID")), Friends.class);
  }
}

this is basic webflux so if you can't do this you need to read up

@GetMapping(path="/friends", produces=MediaType.TEXT_EVENT_STREAM_VALUE)
Flux<Friends> getFriends(Mono<Principal> principal) {
    return principal.map(principal -> {
        return // Here you do what you need to with the principal
    });
}

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