[英]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.