QUESTION: Spring appears to use different deserialization methods for LocalDate
depending on whether it appears in a @RequestBody
or a request @ReqestParam
- is this correct, and if so, is there a way to configure them to be the same throughout an application?
BACKGROUND: In my @RestController
, I have two methods - one GET, and one POST. The GET expects a request parameter ("date") that is of type LocalDate
; the POST expects a JSON object in which one key ("date") is of type LocalDate
. Their signatures are similar to the following:
@RequestMapping(value = "/entity", method = RequestMethod.GET)
public EntityResponse get(
Principal principal,
@RequestParam(name = "date", required = false) LocalDate date)
@RequestMapping(value = "/entity", method = RequestMethod.POST)
public EntityResponse post(
Principal principal,
@RequestBody EntityPost entityPost)
public class EntityPost {
public LocalDate date;
}
I've configured my ObjectMapper as follows:
@Bean
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
objectMapper.registerModule(new JavaTimeModule());
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
return objectMapper;
}
Which ensures the system accepts LocalDate
in the format yyyy-MM-dd and deserializes it as expected - at least when it is part of a @RequestBody
. Thus if the following is the request body for the POST
{
"date": 2017-01-01
}
The system deserializes the request body into an EntityPost
as expected.
However, that configuration does not apply to the deserialization of the @RequestParam
. As a result, this fails:
// fail!
/entity?date=2017-01-01
Instead, the system appears to expect the format MM/dd/yy. As a result, this succeeds:
// success!
/entity?date=01/01/17
I know I can change this on a parameter-by-parameter basis using the @DateTimeFormat annotation. I know that if I change the signature of the GET method as follows, it will accept the first format:
@RequestMapping(value = "/entity", method = RequestMethod.GET)
public EntityResponse get(
Principal principal,
@RequestParam(name = "date", required = false) @DateTimeFormat(iso=DateTimeFormat.ISO.DATE) LocalDate date)
However, I would prefer if I didn't have to include an annotation for every usage of LocalDate
. Is there any way to set this globally, so that the system deserializes every @RequestParam
of type LocalDate
in the same way?
For reference:
I'm using Spring 4.3.2.RELEASE
I'm using Jackson 2.6.5
@ControllerAdvice
. See Setting default DateTimeFormat Annotation in Spring@RequestParam
processing is all Spring.spring-web-XXX.jar
. So I guess you could call that the "Web" subsystem. It is mainly documented in the "Web MVC framework" chapter of the "Spring Framework Reference Documentation" guide, so perhaps the "Web MVC" subsystem is the answer you're looking for.