spring-data-rest:当查询参数与@DateTimeFormat匹配时,查询给出“Unparseable date”

时间:2017-05-18 23:12:38

标签: spring-data-rest

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

}

接下来是具体的子类患者:

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

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

我用于查询的网址是:

// 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

有什么建议从哪里开始?

1 个答案:

答案 0 :(得分: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的网址的最后一位。这是基于Javadoc中org.springframework.format.annotation.DateTimeFormat枚举DATE_TIME的复制粘贴错误示例。如果真棒的春季团队正在倾听,那么请对Javadoc进行一次小修正。谢谢。