本文介绍了时间戳转换器在 Spring Data Rest 与 Spanner 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在无法工作的 Spring Data Rest 自定义转换器的帮助下将字符串格式的输入时间戳转换为云时间戳.在理解为什么不调用自定义转换器方面需要帮助.

I'm trying to convert the input timestamp which will be in the string format to cloud timestamp with the help of a Spring Data Rest custom converter which is not working. Need an help on the same in understanding why custom converters are not invoked.

输入:http://localhost:8080/apipromocentral/promotionsRequestBody : {"startDateTime": "2019-11-07 15:53:00"}

Input: http://localhost:8080/apipromocentral/promotionsRequestBody : {"startDateTime": "2019-11-07 15:53:00"}

POJO:
@ApiModel
@Data
@AllArgsConstructor
@Table(name = "PROMOTIONS")
public class Promotion {
  /**
   * promotion id
   */
  @ApiModelProperty(notes = "Id of the Promotion", required = true)
  @PrimaryKey
  @Column(name = "PROMO_ID")
  private String promotionId;

  @ApiModelProperty(notes = "Start Date Time of a promotion", allowableValues="yyyy-MM-dd HH:mm:ss", required = true)
  @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
  @Column(name = "START_DATE_TIME")
  private Timestamp startDateTime; //java.sql.timestamp;

}

converter code
@Component
public class TimestampWriteConverter implements Converter<java.sql.Timestamp, Timestamp> {

    @Override
    public Timestamp convert(java.sql.Timestamp sqlTimestamp) {
        //Return com.google.cloud.Timestamp;
        return Timestamp.of(sqlTimestamp);
    }
}

异常"message": "FAILED_PRECONDITION: com.google.api.gax.rpc.FailedPreconditionException: io.grpc.StatusRuntimeException: FAILED_PRECONDITION: 表 PROMOTIONS 中列 START_DATE_TIME 的无效值:预期的时间戳.","trace": "com.google.cloud.spanner.SpannerException: FAILED_PRECONDITION: com.google.api.gax.rpc.FailedPreconditionException: io.grpc.StatusRuntimeException: FAILED_PRECONDITION: 表 PROMOTIONS 中列 START_DATE_TIME 的值无效:预期的时间戳.\r\n\tat com.google.cloud.spanner.SpannerExceptionFactory.newSpannerExceptionPreformatted(SpannerExceptionFactory.java:156)\r\n\tat com.google.cloud.spanner.SpannerExceptionFactory.newSpannerException(SpannerExceptionFactory.java:45)\r\n\tat com.google.cloud.spanner.SpannerExceptionFactory.newSpannerException(SpannerExceptionFactory.java:112)\r\n\tat

exception"message": "FAILED_PRECONDITION: com.google.api.gax.rpc.FailedPreconditionException: io.grpc.StatusRuntimeException: FAILED_PRECONDITION: Invalid value for column START_DATE_TIME in table PROMOTIONS: Expected TIMESTAMP.", "trace": "com.google.cloud.spanner.SpannerException: FAILED_PRECONDITION: com.google.api.gax.rpc.FailedPreconditionException: io.grpc.StatusRuntimeException: FAILED_PRECONDITION: Invalid value for column START_DATE_TIME in table PROMOTIONS: Expected TIMESTAMP.\r\n\tat com.google.cloud.spanner.SpannerExceptionFactory.newSpannerExceptionPreformatted(SpannerExceptionFactory.java:156)\r\n\tat com.google.cloud.spanner.SpannerExceptionFactory.newSpannerException(SpannerExceptionFactory.java:45)\r\n\tat com.google.cloud.spanner.SpannerExceptionFactory.newSpannerException(SpannerExceptionFactory.java:112)\r\n\tat

推荐答案

查看文档,看起来您需要将 TimestampWriteConverter 转换器传递给 ConverterAwareMappingSpannerEntityProcessor.

Looking at the documentation, looks like you need pass the TimestampWriteConverter converter to ConverterAwareMappingSpannerEntityProcessor.

@Configuration
public class ConverterConfiguration {

    @Bean
    public SpannerEntityProcessor spannerEntityProcessor(SpannerMappingContext spannerMappingContext) {
        return new ConverterAwareMappingSpannerEntityProcessor(spannerMappingContext,
                Arrays.asList(new TimestampWriteConverter()),
                Arrays.asList());
    }
}

这篇关于时间戳转换器在 Spring Data Rest 与 Spanner 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-28 10:24