Jordi
Jordi

Reputation: 23247

Spring boot: Receive LocalDateTime or Instant parameter into my endpoint

I need to receive a LocalDateTime or an Instant into my endpoint:

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

Is it possible? Is there any workaround?

Upvotes: 0

Views: 1975

Answers (1)

Mykola Yashchenko
Mykola Yashchenko

Reputation: 5371

You can use @RequestParam annotation and send the date value as a query param. For instance:

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

To enable handling of this in Spring you have to add @DateTimeFormat annotation to your param:

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

Upvotes: 4

Related Questions