java - 使用 Spring @RestController 和 @QuerydslPredicate 来处理 GET With ZonedDateTime 参数

标签 java spring querydsl spring-restcontroller

我正在创建一个端点,它将接收日期以在服务器端进行一些过滤。代码如下所示:

@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 调用端点时:http://localhost:3000/api/invoices?page=0&size=20&sort=id,asc&sort=id&transactionDate=2016-05-09T22:00:00.000 Z&transactionDate=2016-05-17T21:59:59.999Z

抛出异常:

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

解析给定日期 works (on ideone.com)仅使用 ZonedDateTime 对象,但好吧,其他东西可能是错误的。我在 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 给出了预期的效果..

明显的区别是在请求参数中添加了 @DateTimeFormat(iso=ISO.DATE_TIME)。现在我想知道;我如何真正让它与 QueryDslPredicateBuilder 一起工作?我应该以某种方式输入格式提示吗?

最佳答案

在寻找使用 querydsl 参数绑定(bind)的不同方面(使用 > x > 类比较)时,我遇到了以下帖子: Can Spring Data REST's QueryDSL integration be used to perform more complex queries?

其中一个答案暗示了以下内容:

Make sure you add the @DateTimeFormat annotation to the dateOfBirth-property of User so that Spring is able to convert the incoming Strings into LocalDate instances correctly.

这是我的问题的解决方案。我已将 @DateTimeFormat 注释添加到我的模型中,以便:

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

瞧,它起作用了。

关于java - 使用 Spring @RestController 和 @QuerydslPredicate 来处理 GET With ZonedDateTime 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37275911/

相关文章:

java - Session.get 在 hibernate 中工作,同时逐步调试,但在 eclipse 中运行时不工作

spring - Play Framework + Spring Data JPA : LazyInitializationException

html - 使用 SpringFramework3 制作时事通讯(HTML)

java - QueryDSL maven设置项目

jpa - 使用 gradle 为 groovy 生成 Querydsl 代码

ibatis - 是否有任何类似于 MyBatis 中的 Criteria API 的功能,或者任何像 QueryDSL 这样的包装器来提供该功能?

java - 如何忽略登录表单上的Spring数据验证?

java - REST Assured 不接受大括号

java - 为什么java并发测试失败?

spring - @Secured 在 Controller 中不起作用,但拦截 URL 似乎工作正常