【问题标题】:"Unparseable date" error in Spring Data REST when query param matches @DateTimeFormat当查询参数与@DateTimeFormat 匹配时,Spring Data REST 中出现“无法解析的日期”错误
【发布时间】:2017-05-18 23:12:38
【问题描述】:

我正在使用 spring-boot-starter-data-jpa-1.5.2.RELEASE 开发 Spring Boot REST 服务器。我有以下 POJO 类层次结构。

首先是基类Entity:

@javax.persistence.Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Entity implements Serializable {    
        
    @Id
    @Column(name = "id", nullable = false, length = 48)
    public String id = UNINITIALIZED_ID;
    
    /**
     * The timestamp for when this entity was last updated.
     */
    @Column(columnDefinition = "timestamp with time zone")
    @Temporal(TemporalType.TIMESTAMP)
    public Date updateTimestamp;
    
}

接下来是具体的子类Patient

@javax.persistence.Entity
@Table(indexes={@Index(columnList="updateTimestamp")})
public class Patient extends Entity {
...
}

我使用自定义方法将PatientRepository 接口定义如下,以获取updateTimestamp 在指定时间戳之后的患者:

@RepositoryRestResource(collectionResourceRel = "patients", path = "patients")
public interface PatientRepository extends JpaRepository<Patient, String> {
    List<Patient> findByUpdateTimestampAfter(@DateTimeFormat(iso=DateTimeFormat.ISO.DATE_TIME)@Param("after")Date after);
}

由于某种未知原因,我在执行 REST 查询时收到日期解析错误,即使我指定时间戳的格式与通过 @DateTimeFormat(iso=DateTimeFormat.ISO.DATE_TIME) 在查询方法中指定的格式一致

我用于查询的 URL 是:

// url example. DateFormat matches @DateTimeFormat on param in query method. 
GET http://127.0.0.1:8090/patients/search/findByUpdateTimestampAfter?after=2030-01-10T00:00:00.000-05:00

我的服务器中的堆栈跟踪是:

Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat @org.springframework.data.repository.query.Param java.util.Date] for value '2030-01-10T00:00:00.000-05:00'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2030-01-10T00:00:00.000-05:00]
    at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:43) ~[spring-core-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:203) ~[spring-core-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.data.repository.support.ReflectionRepositoryInvoker.convert(ReflectionRepositoryInvoker.java:250) ~[spring-data-commons-1.13.1.RELEASE.jar:na]
    ... 59 common frames omitted
Caused by: java.lang.IllegalArgumentException: Parse attempt failed for value [2030-01-10T00:00:00.000-05:00]
    at org.springframework.format.support.FormattingConversionService$ParserConverter.convert(FormattingConversionService.java:204) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.format.support.FormattingConversionService$AnnotationParserConverter.convert(FormattingConversionService.java:320) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:37) ~[spring-core-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    ... 61 common frames omitted
Caused by: java.text.ParseException: Unparseable date: "2030-01-10T00:00:00.000-05:00"
    at java.text.DateFormat.parse(DateFormat.java:366) ~[na:1.8.0_60]
    at org.springframework.format.datetime.DateFormatter.parse(DateFormatter.java:157) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.format.datetime.DateFormatter.parse(DateFormatter.java:43) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.format.support.FormattingConversionService$ParserConverter.convert(FormattingConversionService.java:198) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    ... 63 common frames omitted

有什么建议可以从这里去吗?

【问题讨论】:

    标签: java spring spring-data-rest


    【解决方案1】:

    使用带有以下代码的测试程序,我能够看到我的愚蠢行为:

    try {
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        String str = dateFormat.format(now);
        Date date = dateFormat.parse(str);
    } catch (Exception ex) {
        Logger.getLogger(Application.class.getName()).log(Level.SEVERE, null, ex);
    }
    

    问题是我将Timezone 指定为05:00 而不是0500 的URL 的最后一位。这是基于在 Javadoc 中为 org.springframework.format.annotation.DateTimeFormat 枚举 DATE_TIME 复制粘贴不正确的示例。如果很棒的 spring 团队在听,那么请对 Javadoc 做一个小的更正。谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-03
      • 2020-10-05
      • 2015-05-09
      • 2020-02-17
      • 1970-01-01
      • 2014-09-07
      相关资源
      最近更新 更多