【发布时间】: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