简体   繁体   English

webflux 如何将当前登录 Mono 字符串传递给通量

[英]webflux how to pass current login Mono string to a flux

I am still new to Spring boot Webflux.我还是 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.我使用 Casssandra 作为我的数据库,下面我有一个朋友表,我想根据我的登录 ID 获取我所有的朋友。 How can i get the current login ID and pass the ID to a flux如何获取当前登录 ID 并将 ID 传递给通量

// 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这是基本的 webflux 所以如果你不能这样做,你需要阅读

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

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

相关问题 如何通过Spring WebFlux Reactive方法在处理函数中使用Mono和Flux - How to use Mono and Flux in handler functions with Spring WebFlux Reactive ways 如何制作 Mono/Flux 链并在 sping-webflux 中实现 Retry - How to make chain of Mono/Flux and implement Retry in sping-webflux WebFlux - 如何检查 Mono <responseentity<flux<myitem> &gt;&gt; 为空返回 404 </responseentity<flux<myitem> - WebFlux - how to check if Mono<ResponseEntity<Flux<MyItem>>> is empty to return 404 Spring WebFlux:合并 Mono 和 Flux / 将 Mono 放入 Flux? - Spring WebFlux: Merge Mono and Flux / Put Mono into Flux? Mono如何转换<list<string> > 进入助焊剂<string></string></list<string> - How to convert Mono<List<String>> into Flux<String> 为什么要通过 Mono<ServerResponse> 来自 webflux 路由器中的处理程序函数而不是 Flux<ServerResponse> - Why pass Mono<ServerResponse> from handler function in webflux router and not Flux<ServerResponse> Spring WebFlux,单元测试 Mono 和 Flux - Spring WebFlux, unit testing Mono and Flux 如何转换列表<mono<string> &gt; 进入通量<list<string> &gt;? </list<string></mono<string> - How to convert List<Mono<String>> into Flux<List<String>>? 在Flux / Mono中调用异步任务(Spring boot webflux) - Call async task in Flux/Mono (Spring boot webflux) 春季WebFlux | 静态代码分析工具可检测未使用的Mono / Flux - Spring WebFlux | Static Code Analysis Tool to detect unused Mono / Flux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM