I want to configure Jackson instead using DateTimeFormat.ISO.DATE every time when I'm requesting a date for example:
@RequestMapping(value = "income")
public ResponseEntity calculateIncome(
@RequestParam(value = "companyName") String companyName,
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
@RequestParam(value = "startDate") LocalDate startDate,
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
@RequestParam(value = "endDate") LocalDate endDate
)
I've already tried setup it in JacksonConfig
mapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
or
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
also
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
or
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
even in application.properties i tried
spring.jackson.serialization.write_dates_as_timestamps=true
I'm using spring-boot wit following dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.10.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>
I just don't want to repeat over and over the same @DataTimeFormat but without it, I'm still getting error:
in IntelJ
2018-03-01 15:35:05.539 WARN 8168 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to bind request element: org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam java.time.LocalDate] for value '2018-02-28'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2018-02-28]
Postman
{
"timestamp": 1519914905555,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.web.method.annotation.MethodArgumentTypeMismatchException",
"message": "Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam java.time.LocalDate] for value '2018-02-28'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2018-02-28]",
"path": "/incVat"
}
or
{
"timestamp": "2018-03-01T15:36:44.823+0000",
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.web.method.annotation.MethodArgumentTypeMismatchException",
"message": "Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam java.time.LocalDate] for value '2018-02-28'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2018-02-28]",
"path": "/incVat"
}
SOLUTION:
I Have found the solution here
It's all about a custom editor for LocalDate
and implementation of Formatter
@Bean
public Formatter<LocalDate> localDateFormatter() {
return new Formatter<LocalDate>() {
@Override
public LocalDate parse(String text, Locale locale) throws ParseException {
return LocalDate.parse(text, DateTimeFormatter.ISO_DATE);
}
@Override
public String print(LocalDate object, Locale locale) {
return DateTimeFormatter.ISO_DATE.format(object);
}
};
}