java date iso,使用DateTimeFormat.ISO.DATE配置Jackson而不是工作
我想在每次請求日期時使用DateTimeFormat.ISO.DATE配置Jackson,例如:
@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
)
我已經(jīng)嘗試過在JacksonConfig中設(shè)置它
mapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
要么
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
也
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
要么
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
甚至在我試過的application.properties中
spring.jackson.serialization.write_dates_as_timestamps=true
我正在使用spring-boot機(jī)智跟隨依賴項
org.springframework.boot
spring-boot-starter-web
1.5.10.RELEASE
com.fasterxml.jackson.datatype
jackson-datatype-jsr310
${jackson.version}
我只是不想重復(fù)相同的@DataTimeFormat,但沒有它,我仍然會收到錯誤:
in IntelJ
2018-03-01 15:35:05.539 WARN 8168 --- [nio-8080-exec-1] .wsmsDefaultHandlerExceptionResolver:無法綁定請求元素:org.springframework.web.method.annotation.MethodArgumentTypeMismatchException:轉(zhuǎn)換失敗類型'java.lang.String'的值為必需類型'java.time.LocalDate';嵌套異常是org.springframework.core.convert.ConversionFailedException:無法從類型[java.lang.String]轉(zhuǎn)換為類型[@ org.springframework.web.bind.annotationParam java.time.LocalDate]的值為'2018 -02-28' ;嵌套異常是java.lang.IllegalArgumentException:解析嘗試失敗的值[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"
}
要么
{
"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"
}
總結(jié)
以上是生活随笔為你收集整理的java date iso,使用DateTimeFormat.ISO.DATE配置Jackson而不是工作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 刚晋升为部门经理,我要如何领导下属影响上
- 下一篇: GPT格式的磁盘扩容