gpt4 book ai didi

java - webflux如何将当前登录Mono字符串传递给flux

转载 作者:行者123 更新时间:2023-12-01 18:56:35 25 4
gpt4 key购买 nike

我对 Spring boot Webflux 还很陌生。我使用 Casssandra 作为我的数据库,下面有一个 friend 表,我想根据我的登录 ID 获取我所有的 friend 。我如何获取当前登录ID并将ID传递给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)
}

或者

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

最佳答案

这是基本的 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
});
}

关于java - webflux如何将当前登录Mono字符串传递给flux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59669197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com