I am developing a REST controller in Spring. One of the requirements is to be able to handle dates only in ISO-8601 format.
I'm using Instant
for this purpose. When used in request DTOs, it handles incoming dates perfectly. But when used in response DTOs, the output format needs a little change.
The date in json output looks like this:
2022-02-03T15:00:00Z
But it should look like this:
2022-02-03T15:00:00.000Z
I want to specify how RestController
formats Instant
. To that end, I can think of several solutions.
- Is there a way to override how
RestController
converts a specific class to string? Perhaps in the same way that exception handling is done (with@ExceptionHandler
). - Is there a way to intercept the DTO after it gets converted to json? Before getting sent off to the client, that is.
- Is there a way to simply annotate the return value of the rest controller's method to achieve the desired format?