Deserialize OffsetDataTime java 17

1.3k Views Asked by At

I test endpoints' basic security.

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

The problem is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type java.time.OffsetDateTime not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling (through reference chain: Car field ["DateOfProducing"])

I tried different solutions:

  1. to add com.fasterxml.jackson.datatype:jackson-datatype-jsr310 to dependencies
  1. objectMapper.registerModule(new JavaTimeModule());
  2.  .findAndAddModules()
     .build();```
    
  3. tried serialize object through Gson and register TypeAdapter
     builder.registerTypeAdapter(OffsetDateTime.class, new JSON.OffsetDateTimeTypeAdapter());```
    
    

The service where I write tests uses java 17 but Car Entity is From another repository which uses java 8. It seems to me that the problem does't relate to my test but relates to Car's repository dependencies. TQ, for any piece of advice.

1

There are 1 best solutions below

0
On

resolved by

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