How to set format of string for java.time.Instant using objectMapper?

后端 未结 7 772
失恋的感觉
失恋的感觉 2020-11-29 04:37

I have an entity with java.time.Instant for created data field:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public         


        
相关标签:
7条回答
  • 2020-11-29 05:18

    In my case it was enough to register the JavaTimeModule:

      ObjectMapper objectMapper = new ObjectMapper();
      JavaTimeModule module = new JavaTimeModule();
      objectMapper.registerModule(module);
    
      messageObject = objectMapper.writeValueAsString(event);
    

    In the event Object I have a field of type Instant.

    In the deserialization you also need to register the java time module:

    ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule());
    
    Event event = objectMapper.readValue(record.value(), Event.class);
    
    0 讨论(0)
提交回复
热议问题