Java无法将reactor.core.publisher.MonoDefer强制转换为EntityClass
在这里,我尝试使用返回Publisher的reactiveredisJava无法将reactor.core.publisher.MonoDefer强制转换为EntityClass,java,spring,rx-java,project-reactor,Java,Spring,Rx Java,Project Reactor,在这里,我尝试使用返回Publisher的reactiveredisreactiveedistemplate升级我的spring数据redisRedisTemplate。在本例中,我想将方法findCache更改为Mono。问题是旧的findCache函数使用spring data redis接受通用数据,如下所示: @Autowired ReactiveRedisTemplate redisTemplate; public <T> T findCache(String key,
reactiveedistemplate
升级我的spring数据redisRedisTemplate
。在本例中,我想将方法findCache
更改为Mono
。问题是旧的findCache
函数使用spring data redis接受通用数据,如下所示:
@Autowired
ReactiveRedisTemplate redisTemplate;
public <T> T findCache(String key, Class<T> clazz) {
Object content = this.redisTemplate.opsForValue().get(key);
if (content != null) {
return clazz.cast(content);
}
return null;
}
if (content != null) {
return ((Mono) content).flatMap(o -> clazz.cast(o));
}
public <T> Mono<T> findCache(String key, Class<T> clazz) {
@SuppressWarnings("unchecked")
Mono<Object> contentMono = redisTemplate.opsForValue().get(key);
return contentMono.map(clazz::cast);
}
然后,因为我想让它反应性地工作,所以我更新此代码以返回publisher,如下所示:
@Autowired
ReactiveRedisTemplate redisTemplate;
public <T> T findCache(String key, Class<T> clazz) {
Object content = this.redisTemplate.opsForValue().get(key);
if (content != null) {
return clazz.cast(content);
}
return null;
}
if (content != null) {
return ((Mono) content).flatMap(o -> clazz.cast(o));
}
public <T> Mono<T> findCache(String key, Class<T> clazz) {
@SuppressWarnings("unchecked")
Mono<Object> contentMono = redisTemplate.opsForValue().get(key);
return contentMono.map(clazz::cast);
}
但它也不起作用,因为我的findCache
接受泛型。
如果我需要做什么,请帮助。最好指定
ReactiveRedisTemplate
参数。但如果不能,您应该将内容类型更改为Mono
。大概是这样的:
@Autowired
ReactiveRedisTemplate redisTemplate;
public <T> T findCache(String key, Class<T> clazz) {
Object content = this.redisTemplate.opsForValue().get(key);
if (content != null) {
return clazz.cast(content);
}
return null;
}
if (content != null) {
return ((Mono) content).flatMap(o -> clazz.cast(o));
}
public <T> Mono<T> findCache(String key, Class<T> clazz) {
@SuppressWarnings("unchecked")
Mono<Object> contentMono = redisTemplate.opsForValue().get(key);
return contentMono.map(clazz::cast);
}
公共Mono findCache(字符串键,类clazz){
@抑制警告(“未选中”)
Mono-contentMono=redisTemplate.opsForValue().get(键);
返回contentMono.map(clazz::cast);
}
如果缓存不包含给定键的值,它将返回空的Mono,而不是null。它仍然不工作,Alex先生,我得到了
reactor.core.publisher.monmap不能转换为Person
,还有其他可能的猜测吗?如何设置缓存的值?您应该设置Person对象,而不是Mono