spring-data-rest:查询给出“不可解析的日期";当查询参数匹配@DateTimeFormat [英] spring-data-rest: query giving "Unparseable date" when query param matches @DateTimeFormat

查看:40
本文介绍了spring-data-rest:查询给出“不可解析的日期";当查询参数匹配@DateTimeFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 spring-boot-starter-data-jpa-1.5.2.RELEASE 开发 spring-boot REST 服务器.我有以下 POJO 类层次结构.首先是基类实体:

I am developing a spring-boot REST server using spring-boot-starter-data-jpa-1.5.2.RELEASE. I have the following POJO class hierarchy. First the base class 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:

Next the concrete sub-class Patient:

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

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

I define my PatientRepository interface as follows with a custom method to fetch patients whose updateTimestamp is after a specified timestamp:

@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) 在查询方法中指定的格式一致,我也会收到日期解析错误

For some unknown reason I get a Date parse error when I exercise the REST query even though the format I specify the timestamp in is consistent with he format specified on the query method via @DateTimeFormat(iso=DateTimeFormat.ISO.DATE_TIME)

我用于查询的 URL 是:

The URL I use for the query is:

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

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

The stack trace in my server is:

    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

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

Any suggestions where to go from here?

推荐答案

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

Using a test program with following code I was able to see my silly folly:

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 进行小幅更正.谢谢.

The problem was the last bit of the URL where I had Timezone specified as 05:00 instead of 0500. This was based on copy pasting incorrect example in Javadoc for org.springframework.format.annotation.DateTimeFormat enum DATE_TIME. If the awesome spring team is listening then please make a minor correction to the Javadoc. Thank you.

这篇关于spring-data-rest:查询给出“不可解析的日期";当查询参数匹配@DateTimeFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆