Timestamp converter not working in Spring Data Rest with Spanner

て烟熏妆下的殇ゞ 提交于 2021-02-11 18:18:37

问题


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.

Input: http://localhost:8080/apipromocentral/promotions RequestBody : {"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);
    }
}

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


回答1:


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());
    }
}


来源:https://stackoverflow.com/questions/59071133/timestamp-converter-not-working-in-spring-data-rest-with-spanner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!