2016-05-17 7 views
1

Im Erstellen eines Endpunkts, der Daten erhält, um eine Filterung auf der Serverseite durchzuführen. Der Code sieht wie folgt aus:Verwenden von Spring @RestController mit @QuerydslPredicate zum Behandeln von GET mit den Parametern von "ZonedDateTime"

@RequestMapping(value = "/invoices", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) 
@Timed 
public ResponseEntity<List<Invoice>> getAllInvoices(@QuerydslPredicate(root = Invoice.class) Predicate predicate, Pageable pageable) throws URISyntaxException { 
    log.debug("REST request to get a page of Invoices"); 
    Page<Invoice> page = invoiceService.findAll(predicate, pageable); 
    HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/invoices"); 
    return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK); 
} 

Wenn ich versuche, den Endpunkt mit dieser URL aufzurufen: http://localhost:3000/api/invoices?page=0&size=20&sort=id,asc&sort=id&transactionDate=2016-05-09T22:00:00.000Z&transactionDate=2016-05-17T21:59:59.999Z

Diese Ausnahme ausgelöst wird:

java.time.format.DateTimeParseException: Text '2016-05-09T22:00:00.000Z' could not be parsed at index 10 
    at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949) ~[na:1.8.0_91] 
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851) ~[na:1.8.0_91] 
    at java.time.ZonedDateTime.parse(ZonedDateTime.java:597) ~[na:1.8.0_91] 
    at org.springframework.format.datetime.standard.TemporalAccessorParser.parse(TemporalAccessorParser.java:80) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.format.datetime.standard.TemporalAccessorParser.parse(TemporalAccessorParser.java:47) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.format.support.FormattingConversionService$ParserConverter.convert(FormattingConversionService.java:194) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:35) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:192) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.data.querydsl.binding.QuerydslPredicateBuilder.convertToPropertyPathSpecificType(QuerydslPredicateBuilder.java:217) ~[spring-data-commons-1.11.2.RELEASE.jar:na] 
..... etc etc 

Parsen der angegebenen Daten works (on ideone.com) nur mit dem ZonedDateTime Objekt, aber ok, etwas anderes könnte falsch sein. Ich fand diese Frage auf SO: Using Spring @RestController to handle HTTP GET with ZonedDateTime parameters

@RequestMapping(value = "/invoices", params="action", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) 
@Timed 
public ResponseEntity<List<Invoice>> findInvoices(@RequestParam("dt") @DateTimeFormat(iso=ISO.DATE_TIME) ZonedDateTime dt,Pageable pageable) throws URISyntaxException { 
    log.debug("REST request to get a page of Invoices"); 
    Page<Invoice> result = invoiceRepository.findAllByTransactionDate(dt,pageable); 
    HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(result, "/api/invoices"); 
    return new ResponseEntity<>(result.getContent(), headers, HttpStatus.OK); 
} 

die URL anfordern: localhost:8080/api/invoices?action=search&dt=2016-05-13T15:12:33.658Z Gibt den gewünschten Effekt ..

Der offensichtlichste Unterschied ist die @DateTimeFormat(iso=ISO.DATE_TIME) Neben dem requestparam. Jetzt frage ich mich; Wie bekomme ich das eigentlich, um mit dem QueryDslPredicateBuilder zu arbeiten? Soll ich irgendwie das Format tippen?

Antwort

0

Wenn für einen anderen Aspekt der Verwendung querydsl Parameterbindung (mit> x> Art von Vergleich) auf der Suche lief ich in den folgenden Beitrag: Can Spring Data REST's QueryDSL integration be used to perform more complex queries?

Eine der Antworten deuteten folgende:

Stellen Sie sicher, dass Sie die @DateTimeFormat-Annotation der dateOfBirth-Eigenschaft von User hinzufügen, damit Spring die eingehenden Strings in LocalDate-Instanzen korrekt konvertieren kann.

Welches ist die Lösung für mein Problem. Ich habe die @DateTimeFormat Anmerkung zu meinem Modell hinzugefügt, so dass:

@DateTimeFormat(iso = ISO.DATE_TIME) 
@Column(name = "transaction_date") 
private ZonedDateTime transactionDate; 

Und voila, es funktioniert.

+0

Vielleicht können Sie versuchen, wenn die globale Konfiguration: 'spring.jackson.date-format: com.fasterxml.jackson.databind.util.ISO8601DateFormat' den gleichen Effekt hat, und es wäre eine einfachere Art zu erreichen, was Sie sind Auf der Suche nach –

Verwandte Themen