2016-05-17 17 views
1

サーバ側でフィルタリングを行うための日付を受け取るエンドポイントを作成しています。コードは次のようになります。Spring @RestControllerを@QuerydslPredicateと共に使用してZonedDateTimeパラメータでGETを処理する

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

私はこのURLでエンドポイントを呼び出そう:ちょうどZonedDateTimeを使用して、指定された日付works (on ideone.com)の解析

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 

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

この例外がスローされますオブジェクトが、他の何かが間違っている可能性があります。私は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); 
} 

URLを要求:localhost:8080/api/invoices?action=search&dt=2016-05-13T15:12:33.658Zが所望の効果を与えます。..

明らかな違いがrequestparamに@DateTimeFormat(iso=ISO.DATE_TIME)追加です。今私は疑問に思っている。実際にQueryDslPredicateBuilderを使用するにはどうしたらいいですか?私は何とかフォーマットをタイプヒントする必要がありますか?

答えて

0

(比較の> X>種類を使用して)結合querydslパラメータを使用してのさまざまな側面を探して、私は次のポストに走っ:回答 Can Spring Data REST's QueryDSL integration be used to perform more complex queries?

一つは、次のことを示唆した。

@DateTimeFormat注釈を のdateOfBirthプロパティに追加して、Springが 入ってくるStringをLocalDateインスタンスに正しく変換できるようにしてください。

私の問題の解決策はどれですか。モデルに@DateTimeFormatアノテーションを追加しました。

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

そして、それはうまくいきます。

+0

グローバル設定: 'spring.jackson.date-format:com.fasterxml.jackson.databind.util.ISO8601DateFormat'が同じ効果を持っている場合は試してみてください。それはあなたが達成しているより簡単な方法です探している –

関連する問題