繁体   English   中英

SPRING REDIS - 找不到能够从类型转换的转换器.. 错误

[英]SPRING REDIS - No converter found capable of converting from type.. ERROR

我正在使用 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));
  }
}

您需要在 Redis 配置类中配置一个新的 RedisCustomConversions bean,以便让您的应用程序使用您的 BytesToDateConverter 转换器。 请看这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM