繁体   English   中英

反序列化 OffsetDataTime java 17

[英]Deserialize OffsetDataTime java 17

我测试端点的基本安全性。

Test

 mvc.perform(MockMvcRequestBuilders.post(path)
        .contentType(MediaType.APPLICATION_JSON)
        .content(objectMapper.writeValueAsString(OBJECT))
        .params(usersSampleRequestParams)
        .with(httpBasic(user, password)))
        .andExpect(status().isOk());
    enter code here

Controller

@PostMapping("/path")
  public void methodSignature(@RequestBody Car car) {}

问题是com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type java.time.OffsetDateTime not supported by default: add Module "com.fasterxml.88156758datatype-4" to enablejackson3.处理(通过引用链:Car 字段 ["DateOfProducing"])

我尝试了不同的解决方案:

  1. 将 com.fasterxml.jackson.datatype:jackson-datatype-jsr310 添加到依赖项
  1. objectMapper.registerModule(new JavaTimeModule());
  2.  .findAndAddModules().build();```
  3. 尝试序列化 object 到 Gson 并注册 TypeAdapter
     builder.registerTypeAdapter(OffsetDateTime.class, new JSON.OffsetDateTimeTypeAdapter());```

我编写测试的服务使用 java 17,但 Car Entity 来自另一个使用 java 8 的存储库。在我看来,问题与我的测试无关,但与 Car 的存储库依赖关系有关。 TQ,任何建议。

解决了

 @Bean
  @Primary
  public ObjectMapper objectMapper() {
    return new ObjectMapper()
        .registerModule(new JavaTimeModule();
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM