为什么消息转换器不使用 Objectmapper 日期格式进行日期转换?

why is the Object Mapper date fromat not used by message converters for date transform?

我遇到了 JSON 的自定义日期格式问题,它在测试中当然有效,但在部署的应用程序中失败。我想将日期模式用作 dd-MM-yyyy 几乎是标准的,以及它在印度的预期方式。还配置了一个日期格式化程序,并像这样注入到配置中

@Configuration
public class RepositoryWebConfiguration extends RepositoryRestMvcConfiguration {

private static final Logger LOGGER = LoggerFactory.getLogger(RepositoryWebConfiguration.class);

  @Override
  public void configureJacksonObjectMapper(ObjectMapper objectMapper) {
    LOGGER
    .debug("Configuring dd-MM-yyyy as default date pattern for all JSON representations in Rest DATA Repositories");
    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
    builder.indentOutput(true).dateFormat(new SimpleDateFormat("dd-MM-yyyy"));
    objectMapper = builder.build();
  }
}

现在这应该适用于 JSON,因为我正在注入特定的日期格式,在我的测试中,我首先创建一个具有相同格式的映射器

  private ObjectMapper halObjectMapper() {
    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
    builder.indentOutput(true).dateFormat(new SimpleDateFormat("dd-MM-yyyy"));
    ObjectMapper objectMapper = builder.build();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.registerModule(new Jackson2HalModule());
    return objectMapper;
  }

然后我使用此映射器为 POST 请求生成 JSON。 JSON 生成一切正常,我期望格式 dd-MM-yyyy 而我得到的正是

{
  "id":null,
  "name":"KABADI",
  "seatsAvailable":40,
  "workshopType":"KABADI FOUNDATION",
  "date":"16-08-2015",
  "venue":"http://localhost:8080/venues/2"
}

随着 ObjectMapper 的注册,我希望这个 JSON 可以毫无问题地转换为 Workshop 对象并且日期格式为 dd-MM-yyyy。但是,POST 请求由于格式异常而失败,Jackson 抱怨它无法将 dd-MM-yyyy 转换为 Date,因为可用格式为 "only"

("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd")

这是日志

Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of java.util.Date from String value '16-08-2015': not a valid representation (error: Failed to parse Date value '16-08-2015': Can not parse date "16-08-2015": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))
 at [Source: HttpInputOverHTTP@54153158; line: 5, column: 53] (through reference chain: com.agilityroots.doi.workshop.entity.Workshop["date"])
  at com.fasterxml.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:55)
  at com.fasterxml.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:810)
  at com.fasterxml.jackson.databind.deser.std.StdDeserializer._parseDate(StdDeserializer.java:740)
  at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateBasedDeserializer._parseDate(DateDeserializers.java:176)
  at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateDeserializer.deserialize(DateDeserializers.java:262)
  at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateDeserializer.deserialize(DateDeserializers.java:246)
  at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:538)
  at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:99)
  at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:238)
  at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118)
  at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3066)
  at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2221)
  at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:205)
  ... 46 common frames omitted

在通常的 Spring WebMVC + Boot 场景中,以前不必过多地研究这些覆盖,这个 属性 用来解决问题

spring.jackson.date-format=dd-MM-yyyy

所以我可能在这里遗漏了一些东西,或者我以错误的方式配置了 objectMapper,因为它没有被注入?如何让 JSON 转换器接受 dd-MM-yyyy 格式?

做的太多了,这暂时修复了测试。 JSON 的对象仍然不是 dd-MM-yyyy 格式,被解释为一个单独的日期,例如 16-08-2015 returns 因为 February X in 22 会调查

  @Override
  public void configureJacksonObjectMapper(ObjectMapper objectMapper) {

    objectMapper.setDateFormat(new SimpleDateFormat("dd-MM-yyyy"));
  }

更新:

另一个问题是在 Workshop 中用 @Temporal(TemporalType.DATE) 注释日期字段导致日期被保存为 2015-08-16,也删除了该注释。