使用 DateTimeFormat.ISO.DATE 配置 Jackson - 不工作

luk*_*go3 5 java jackson spring-boot jackson-dataformat-xml

我想在每次请求日期时使用 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
  ) 
Run Code Online (Sandbox Code Playgroud)

我已经尝试在 JacksonConfig 中设置它

mapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
Run Code Online (Sandbox Code Playgroud)

或者

mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
Run Code Online (Sandbox Code Playgroud)

mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
Run Code Online (Sandbox Code Playgroud)

或者

mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
Run Code Online (Sandbox Code Playgroud)

即使在 application.properties 我试过

spring.jackson.serialization.write_dates_as_timestamps=true
Run Code Online (Sandbox Code Playgroud)

我正在使用具有以下依赖项的 spring-boot

 <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>
Run Code Online (Sandbox Code Playgroud)

我只是不想一遍又一遍地重复相同的 @DataTimeFormat 但没有它,我仍然收到错误:

在 IntelJ

2018-03-01 15:35:05.539 WARN 8168 --- [nio-8080-exec-1] .wsmsDefaultHandlerExceptionResolver:无法绑定请求元素:org.springframework.web.method.annotation.MethodArgumentTypeMismatchException:无法转换值输入'java.lang.String' 到所需的类型'java.time.LocalDate';嵌套异常是 org.springframework.core.convert.ConversionFailedException:无法从类型 [java.lang.String] 转换为类型 [@org.springframework.web.bind.annotation.RequestParam java.time.LocalDate] 的值 '2018 -02-28'; 嵌套异常是 java.lang.IllegalArgumentException:解析尝试失败的值 [2018-02-28]

邮差

{
    "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"
}
Run Code Online (Sandbox Code Playgroud)

或者

{
    "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"
}
Run Code Online (Sandbox Code Playgroud)