目前我是新的响应式编程,我已经在2个文档中添加了数据,所以目前我要做的是只将这些数据返回给客户端,其tokenIdentifier在两个文档中都是相同的。
请参考下面的代码:
我有两个集合
package com.mainApp;
import java.util.Date;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(value = "Token")
public class TokenData {
@Id
private String id;
private String tokenIdentifier;
private Date todayDate;
public TokenData(String id, String tokenIdentifier, Date todayDate) {
super();
this.id = id;
this.tokenIdentifier = tokenIdentifier;
this.todayDate = todayDate;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTokenIdentifier() {
return tokenIdentifier;
}
public void setTokenIdentifier(String tokenIdentifier) {
this.tokenIdentifier = tokenIdentifier;
}
public Date getTodayDate() {
return todayDate;
}
public void setTodayDate(Date todayDate) {
this.todayDate = todayDate;
}
}
package com.mainApp;
import java.util.Date;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(value = "TURCollection")
public class TURCollection {
@Id
private String id;
private String turIdentifier;
private String tokenIdentifier;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTurIdentifier() {
return turIdentifier;
}
public void setTurIdentifier(String turIdentifier) {
this.turIdentifier = turIdentifier;
}
public String getTokenIdentifier() {
return tokenIdentifier;
}
public void setTokenIdentifier(String tokenIdentifier) {
this.tokenIdentifier = tokenIdentifier;
}
}
我有一个控制器,它将只返回那些tokenData. gettokenidentifier () == TURCollection.getTokenIdentifier()的tokenData。
@GetMapping(value = "/getAllToken")
public Flux<TokenData> getToken(){
/*List<TokenData> returnData = new ArrayList<TokenData>();
List<TokenData> tokenData = tokenDataRepository.findAll().collectList().block();
List<TURCollection> turCollection = turRepository.findAll().collectList().block();
turCollection.forEach(tur -> {
for(TokenData data : tokenData) {
if(tur.getTokenIdentifier().equals(data.getTokenIdentifier())) {
returnData.add(data);
}
}
});*/
但是block()代码在响应式编程中不起作用谁能帮我如何用反应的方式比较两个通量的值?
可以使用Flux的组合。collectList和mono_flatmap .
Mono<List<TokenData>> tokenData = tokenDataRepository.findAll().collectList();
Mono<List<TURCollection>> turCollection = turRepository.findAll().collectList();
Mono<List<TokenData>> result = turCollection.flatMap(turs ->
Set<String> ids = turs.stream().map(TURCollection::getId).collect(toSet());
return tokenData.map(tokens ->
tokens.stream().filter(token -> ids.contains(token.getId())).collect(toList())
);
);