【发布时间】:2019-12-28 01:51:18
【问题描述】:
我正在使用 spring redis,当我尝试从 redis 检索实体时出现此错误。问题出在 AuditedEntiy 类的 lastModifiedTime 字段中。
{error: "No converter found capable of converting from type [byte[]] to type [java.sql.Timestamp]"}
我已经添加了一个从字节数组到时间戳的转换器,但问题仍然存在。当前实现有一个端点将实体存储在redis中,另一个端点将存储的redis实体存储在关系数据库(mysql)中并更新redis实体中的id(我们为存储在redis和数据库中的实体维护相同的id ),在这一步之后,当我们尝试从 redis 获取实体时,我得到了上面提到的错误。
redis 实例托管在亚马逊上,发生的一件奇怪的事情是,当我运行本地 Spring Boot 应用程序时它可以工作,但在亚马逊托管实例上我收到此错误。
数据库实体:
@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;
}
}
已添加到日期转换器的字节数:
@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));
}
}
【问题讨论】: