Home > Mobile >  How to deserialize java.time.Instant in Jackson2JsonRedisSerializer
How to deserialize java.time.Instant in Jackson2JsonRedisSerializer

Time:01-02

I am using Jackson2JsonRedisSerializer.

@Bean
    public RedisOperations<String , Aircraft> redisOperations(RedisConnectionFactory factory){
        Jackson2JsonRedisSerializer<Aircraft> serializer = new Jackson2JsonRedisSerializer<Aircraft>(Aircraft.class);
        RedisTemplate<String, Aircraft> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        template.setDefaultSerializer(serializer);
        template.setKeySerializer(new StringRedisSerializer());
        return template;
    }

My aircraft has field of type Instant.

    @JsonProperty("last_seen_time")
    private Instant lastSeenTime;

While deserializing it gives me an error:

Java 8 date/time type `java.time.Instant` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling (through reference chain: com.example.chapter6.sburredis.Aircraft["last_seen_time"])

I have already added the maven dependency in my pom.xml

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

The json file is as follows:

{
  "aircraft": [
    {
      "id": 108,
      "callsign": "AMF4263",
      "squawk": "4136",
      "reg": "N49UC",
      "flightno": "",
      "route": "LAN-DFW",
      "type": "B190",
      "category": "A1",
      "altitude": 20000,
      "heading": 235,
      "speed": 248,
      "lat": 38.865905,
      "lon": -90.429382,
      "barometer": 0,
      "vert_rate": 0,
      "selected_altitude": 0,
      "polar_distance": 12.99378,
      "polar_bearing": 345.393951,
      "is_adsb": true,
      "is_on_ground": false,
      "last_seen_time": 1672576769,
      "pos_update_time": 1672576769,
      "bds40_seen_time": null
      }
  ]
}

CodePudding user response:

This error occurs when we serialize a Java object or deserialize JSON to POJO. The POJO contains new Java 8 date time classes such as LocalDate, LocalTime, LocalDateTime etc.
Steps:

  1. Add dependency
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
  1. Register the module JavaTimeModule either with ObjectMapper or JsonMapper
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());

// If JsonMapper is used in application
JsonMapper jsonMapper = new JsonMapper();
jsonMapper.registerModule(new JavaTimeModule());

Here you are using Jackson2JsonRedisSerializer in the bean. So you can create the ObjectMapper object and set the JavaTimeModule. After that register the ObjectMapper to the Jackson2JsonRedisSerializer object.


ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
  • Related