spring-data-rest: query giving “Unparseable date” when query param matches @DateTimeFormat

我的未来我决定 提交于 2019-12-11 05:04:10

问题


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;

}

Next the concrete sub-class Patient:

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

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

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)

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?


回答1:


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

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.



来源:https://stackoverflow.com/questions/44059153/spring-data-rest-query-giving-unparseable-date-when-query-param-matches-date

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