2017-05-18 1 views
1

Je développe un serveur REST spring-boot en utilisant spring-boot-starter-data-jpa-1.5.2.RELEASE. J'ai la hiérarchie de classe POJO suivante. Tout d'abord la classe de base Entité:spring-data-rest: requête donnant une date impossible à paramétrer lorsque le paramètre de requête correspond à @DateTimeFormat

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

} 

Suivant la sous-classe concrète du patient:

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

je définis mon interface PatientRepository comme suit avec une méthode personnalisée pour aller chercher les patients dont updateTimestamp est après un horodatage spécifié:

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

Pour une raison inconnue, je reçois une erreur d'analyse date quand je l'exercice de la requête REST même si le format que je spécifie l'horodatage en est l'esprit cohérent h il format spécifié sur la méthode de requête via @DateTimeFormat (iso = DateTimeFormat.ISO.DATE_TIME)

L'URL J'utilise pour la requête est:

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

La trace de pile dans mon serveur est:

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 

Des suggestions où aller?

Répondre

1

L'utilisation d'un programme de test avec le code suivant, j'ai pu voir ma folie idiote:

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

Le problème a été le dernier bit de l'URL où j'avais Timezone spécifié comme 05:00 au lieu de 0500. Cela était basé sur copier un exemple incorrect dans Javadoc pour org.springframework.format.annotation.DateTimeFormat enum DATE_TIME. Si l'équipe printanière est à l'écoute, veuillez apporter une correction mineure au Javadoc. Je vous remercie.