spring-cloud-openfeign: Feign client doesn't serialize java.time.LocalDate's correctly while it present as object field
I’m using Spring boot 2.4.5, Spring Cloud 2020.0.3, Java 11.
Is there a way to use a Feign client accepting a java.time.LocalDate
as an object field where it is supposed to comply with a given format like @DateTimeFormat
with some specific pattern?
For instance, I have the next object which I wanna represent as query params:
@AllArgsConstructor
public class QueryRequestParams {
@DateTimeFormat(pattern = "dd.MM.yyyy")
private LocalDate from;
@DateTimeFormat(pattern = "dd.MM.yyyy")
private LocalDate to;
}
The instance of QueryRequestParams looks like:
new QueryRequestParams(LocalDate.parse("2011-12-03"), LocalDate.parse("2011-12-05"))
The feign client is:
@FeignClient(url = "http://localhost:9000")
public interface ExampleFeignClient {
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
List<Object> getObjects(@SpringQueryMap QueryRequestParams queryRequestParams);
}
When feign client does request and
QueryRequestParams` object has no null values the request look like as “http://localhost:9000?from=2011-12-03&to=2011-12-05” which has another format rather than I configured via DateTimeFormat annotation.
When I tried to use @DateTimeFormat with @RequestParam annotation just in ExampleFeignClient, for example:
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
List<Object> getObjects(@RequestParam("from") @DateTimeFormat(pattern = "dd.MM.yyyy") LocalDate from,
@RequestParam("to") @DateTimeFormat(pattern = "dd.MM.yyyy") LocalDate to);
then it works as I expected.
It there a way to use @SpringQueryMap
with object which contains LocalDate
field and that is formatted via @DateTimeFormat(pattern = "dd.MM.yyyy")
annotation?
Thanks for any help!
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 2
- Comments: 15 (3 by maintainers)
I don’t understand why has this issue been closed. What kind of feedback was expected to be provided?
From my side, I can confirm that
@DateTimeFormat(pattern = "..."
on@SpringQueryMap
-annotated parameter is ignored by the client and resulting request consists of default ISO date format, as if@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
was given.@cbezmen, but this way works fine
Here also @DateTimeFormat annotation with LocalDate type, just presented as separate param, not field of object
Hi @cbezmen, but your date formation is also different from the pattern. I expect the date to be shaped like dd.MM.yyy when this date field is represented as an object field and I using this like: