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

时间:2018-03-01 15:34:56

标签: java spring-boot jackson 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
  ) 

我已经尝试过在JacksonConfig中设置它

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机智跟随依赖

 <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>

我只是不想重复同样的@DataTimeFormat 但没有它,我仍然会收到错误:

在IntelJ

  

2018-03-01 15:35:05.539 WARN 8168 --- [nio-8080-exec-1]   .w.s.m.s.DefaultHandlerExceptionResolver:无法绑定请求   元件:   org.springframework.web.method.annotation.MethodArgumentTypeMismatchException:   无法转换类型&#39; java.lang.String&#39;的值要求的类型   &#39; java.time.LocalDate&#39 ;;嵌套异常是   org.springframework.core.convert.ConversionFailedException:失败   从类型[java.lang.String]转换为type   [@ org.springframework.web.bind.annotation.RequestParam   java.time.LocalDate] for value&#39; 2018-02-28&#39 ;;嵌套异常是   java.lang.IllegalArgumentException:解析尝试失败的值   [2018年2月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"
}

{
    "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"
}

2 个答案:

答案 0 :(得分:1)

你需要在你的pom.xml中使用它:

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
</dependency>

在您的Configuration类中:

@Configuration
public class WebConfigurer {

    @Bean
    @Primary // pay attention on this
    public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.build();
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        return objectMapper;
    }
}

Jackson2Json配置:

@Configuration
public class Jackson2JsonConfiguration {
    @Bean
    public Jackson2JsonObjectMapper jackson2JsonObjectMapper(ObjectMapper objectMapper) {
        return new Jackson2JsonObjectMapper(objectMapper);
    }
}

如果不起作用,请尝试:

  • 更改pom.xml中jsr310依赖项导入的顺序。
  • 添加属性文件:jackson.serialization.write-dates-as-timestamps = false

我尝试做这项工作时遇到了奇怪的问题。

作为最后一个选项,您可以迁移到Spring Boot 2.0,因为他们将Jackson配置默认设置为将JSR-310日期写为ISO-8601字符串。

答案 1 :(得分:0)

<强>解: 我找到了解决方案here 这完全是关于LocalDate的自定义编辑器和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);
      }
    };
  }