春季启动:接收LocalDateTime或Instant参数到我的端点

时间:2018-10-23 07:50:36

标签: spring spring-boot

我需要在我的端点中收到LocalDateTimeInstant

@RequestMapping(
    path = Constants.AUDITS_MAPPING,
    method = RequestMethod.GET,
    produces = MediaType.APPLICATION_JSON_VALUE
)
public Collection<Audit> listPendingAudits(Instant /*or LocalDateTime*/ deadline);

有可能吗?有什么解决方法吗?

1 个答案:

答案 0 :(得分:4)

您可以使用@RequestParam批注并将日期值作为查询参数发送。例如:

GET http://localhost:8080/api/resource?date=2018-10-31T01:30:00.000

要在Spring中启用此功能,您必须在参数中添加@DateTimeFormat批注:

@RequestMapping(
    path = Constants.AUDITS_MAPPING,
    method = RequestMethod.GET,
    produces = MediaType.APPLICATION_JSON_VALUE
)
public Collection<Audit> listPendingAudits(@RequestParam(value = "date", required = false)
                                           @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime deadline);