Java SPRING REDIS-未找到能够从类型转换的转换器。。错误
我使用的是SpringRedis,当我试图从redis中检索实体时,出现了这个错误。问题位于AuditedEntity类的lastModifiedTime字段中Java SPRING REDIS-未找到能够从类型转换的转换器。。错误,java,spring,redis,Java,Spring,Redis,我使用的是SpringRedis,当我试图从redis中检索实体时,出现了这个错误。问题位于AuditedEntity类的lastModifiedTime字段中 {error: "No converter found capable of converting from type [byte[]] to type [java.sql.Timestamp]"} 我已经添加了一个从字节数组到时间戳的转换器,但问题仍然存在。当前的实现有一个端点将实体存储在redis中,另一个端点将存储的redis实
{error: "No converter found capable of converting from type [byte[]] to type [java.sql.Timestamp]"}
我已经添加了一个从字节数组到时间戳的转换器,但问题仍然存在。当前的实现有一个端点将实体存储在redis中,另一个端点将存储的redis实体存储在关系数据库(mysql)中,并更新redis实体中的id(我们为存储在redis和DB中的实体维护相同的id),在这一步之后,当我们尝试从redis获取实体时,我得到上述错误
redis实例托管在amazon上一件奇怪的事情是,当我运行本地spring boot应用程序时,它可以工作,但在amazon托管的实例上,我得到了这个错误
DB实体:
@MappedSuperclass
@Audited
@EntityListeners(AuditingEntityListener.class)
public abstract class AuditedEntity {
@CreatedBy
@LastModifiedBy
protected Long lastModifiedBy;
@CreatedDate
@LastModifiedDate
@Temporal(TIMESTAMP)
protected Date lastModifiedTime;
public Long getLastModifiedBy() {
return lastModifiedBy;
}
public void setLastModifiedBy(Long lastModifiedBy) {
this.lastModifiedBy = lastModifiedBy;
}
public Date getLastModifiedTime() {
return lastModifiedTime;
}
public void setLastModifiedTime(Date lastModifiedTime) {
this.lastModifiedTime = lastModifiedTime;
}
}
@Entity
@Audited
@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
public @Data class DomainEntity extends AuditedEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@EqualsAndHashCode.Include
private Long id;
@NotNull
private String name;
private String description;
private Boolean locked = Boolean.FALSE;
@Version
private Integer version;
@NotNull
private Long tenant;
}
REDIS实体:
@RedisHash("pricingAnalysis")
@JsonIgnoreProperties(ignoreUnknown = true)
public @Data class DomainEntityDto {
@Id
@JsonSerialize(using = LongIdToStringSerializer.class)
private Long id;
private String name;
private String description;
private Boolean locked;
private Integer version;
@CustomDateFormat("MM/dd/yyyy hh:mm a z")
@JsonSerialize(using = DateSerializer.class)
@JsonDeserialize(using = DateDeserializer.class)
private Date lastModifiedTime;
private String lastModifiedBy;
private Long tenant;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy")
private LocalDate observationFrom;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy hh:mm a")
private LocalDateTime observationTo;
public PricingAnalysisDto updateObservationPeriod() {
Optional<DateRangeDto> observationPeriod = Optional.ofNullable(getSearch())
.map(SearchDto::getParams).map(ParamsDto::getTrade).map(TradeParamsDto::getObserved)
.filter(obs -> obs.getFrom() != null && obs.getTo() != null);
if (observationPeriod.isPresent()) {
this.setObservationFrom(observationPeriod.get().getFrom().toInstant()
.atZone(ZoneId.systemDefault()).toLocalDate());
this.setObservationTo(observationPeriod.get().getTo().toInstant()
.atZone(ZoneId.systemDefault()).toLocalDateTime());
}
return this;
}
}
@RedisHash(“pricingananalysis”)
@JsonIgnoreProperties(ignoreUnknown=true)
public@Data class DomainEntityDto{
@身份证
@JsonSerialize(使用=LongIdToStringSerializer.class)
私人长id;
私有字符串名称;
私有字符串描述;
私有布尔锁;
私有整数版本;
@CustomDateFormat(“MM/dd/yyyy hh:MM a z”)
@JsonSerialize(使用=DateSerializer.class)
@JsonDeserialize(使用=DateDeserializer.class)
私人日期lastModifiedTime;
私有字符串lastModifiedBy;
私人长期租户;
@JsonFormat(shape=JsonFormat.shape.STRING,pattern=“dd/MM/yyyyy”)
私有LocalDate observationFrom;
@JsonFormat(shape=JsonFormat.shape.STRING,pattern=“dd/MM/yyyy hh:MM a”)
私有LocalDateTime observationTo;
公共PricingAnalysisDto updateObservationPeriod(){
Optional observationPeriod=Optional.ofNullable(getSearch())
.map(SearchDto::getParams).map(ParamsDto::getTrade).map(TradeParamsDto::getObserved)
.filter(obs->obs.getFrom()!=null&&obs.getTo()!=null);
if(observationPeriod.isPresent()){
this.setObservationFrom(observationPeriod.get().getFrom().toInstant())
.atZone(ZoneId.systemDefault()).toLocalDate());
this.setObservationTo(observationPeriod.get().getTo().toInstant())
.atZone(ZoneId.systemDefault()).toLocalDateTime();
}
归还这个;
}
}
已添加字节到日期转换器:
@Component
@ReadingConverter
public class BytesToDateConverter implements Converter<byte[], Timestamp> {
@Override
public Timestamp convert(final byte[] source) {
String value = new String(source);
return new Timestamp(Long.parseLong(value));
}
}
@组件
@读取转换器
公共类ByTestODataConverter实现转换器{
@凌驾
公共时间戳转换(最终字节[]源){
字符串值=新字符串(源);
返回新的时间戳(Long.parseLong(value));
}
}
您需要在Redis配置类中配置一个新的RedisCustomConversions bean,以便应用程序使用ByTestODataConverter转换器。请看