2

Env:Spring Boot\Mybatis-Plus\Jackson the entity of Ime:

package cn.bukkit.sip.orm.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.time.LocalDateTime;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "img", resultMap = "BaseResultMap")
public class ImgEntity implements Serializable {
    @TableId(value = "id", type = IdType.ASSIGN_ID)
    @JsonSerialize(using = ToStringSerializer.class)
    private Long id;

    @TableField(value = "name")
    private String name;

    @TableField(value = "path")
    private String path;

    @TableField(value = "size")
    private Integer size;

    @Builder.Default
    @TableField(value = "public")
    private Boolean isPublic = true;

    @TableField(value = "limit_date")
    private LocalDateTime dateLimit;

    @TableField(value = "limit_times")
    private Integer timesLimit;

    @TableField(value = "owner")
    private String owner;

    @Builder.Default
    @TableLogic
    @TableField(value = "deleted")
    @JsonIgnore
    private Boolean isDeleted = false;

    @JsonIgnore
    @TableField(exist = false)
    final private UserEntity userEntity = null;
}

and i config the bean of object mapper

    @Bean
    ObjectMapper objectMapper() {
        return JsonMapper.builder()
                .addModule(new JavaTimeModule())
                .build();
    }

but when i read the record,and sout the json,the exception:

Could not write JSON: Type id handling not implemented for type java.lang.Object (by serializer of type com.fasterxml.jackson.databind.ser.impl.UnsupportedTypeSerializer) (through reference chain: cn.bukkit.sip.orm.entity.ImgEntity[\"dateLimit\"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: **Type id handling not implemented for type java.lang.Object** (by serializer of type com.fasterxml.jackson.databind.ser.impl.UnsupportedTypeSerializer) (through reference chain: cn.bukkit.sip.orm.entity.ImgEntity[\"dateLimit\"])

why

Type id handling not implemented for type java.lang.Object

i think i had already config the JavaTimeModule

i can enabled it by use

    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8", shape = JsonFormat.Shape.STRING)

but i want to do it global

and i had already tried the Jackson2ObjectMapperBuilderCustomizer

nothing happened!

result: i found it's the redis cache problem,

the code:

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory) {
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

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

        jackson2JsonRedisSerializer.setObjectMapper(om);

        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ZERO)
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
                .disableCachingNullValues();
        return RedisCacheManager.builder(factory)
                .cacheDefaults(config)
                .build();
    }
Zerek
  • 63
  • 7
  • Hi. You should probably use a `Jackson2ObjectMapperBuilderCustomizer` bean instead as shown here: https://www.baeldung.com/spring-boot-customize-jackson-objectmapper. Please note `LOCAL_DATETIME_SERIALIZER`. BR – Roar S. Sep 18 '22 at 12:50
  • @RoarS. already use,but nothing happen ``` @Bean public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() { return builder -> builder.serializationInclusion(JsonInclude.Include.NON_NULL) .serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))) .deserializers(new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); } ``` – Zerek Sep 18 '22 at 13:01
  • Please put a breakpoint inside `jsonCustomizer()` and verify that the code is executed. BR – Roar S. Sep 18 '22 at 13:06
  • @RoarS. yes,i had already confirm it had been executed – Zerek Sep 18 '22 at 13:07
  • @RoarS. it said "Type id handling not implemented for type java.lang.Object" ,is it mean LocalDateTime is recognized as an Object? – Zerek Sep 18 '22 at 13:09
  • Please add a "I've also tried" section to your question with the `Jackson2ObjectMapperBuilderCustomizer` in order to inform others about this. BR – Roar S. Sep 18 '22 at 13:10
  • @RoarS. done, can you help me more? – Zerek Sep 18 '22 at 13:21
  • Found this StackOverflow answer from May this year: https://stackoverflow.com/a/72120597/14072498 – Roar S. Sep 18 '22 at 13:25
  • @RoarS. i try it with ``` @Bean @Primary ObjectMapper objectMapper() { return JsonMapper.builder() .addModule(new JavaTimeModule()) .serializerFactory(new CustomBeanSerializerFactory(null)) .build(); }``` nothing happened – Zerek Sep 18 '22 at 13:41
  • @RoarS.After my testing ObjectMapper can handle LocalDateTime, but this feature is not available in Spring MVC return JSON messages – Zerek Sep 18 '22 at 13:53
  • 1
    @RoarS. thks,i found the problem.It's the redis cache problem – Zerek Sep 18 '22 at 14:23

1 Answers1

0

I resolved this issue by using

SerializationFeature.WRITE_DATES_AS_TIMESTAMPS 

so, try to change you jackson mapper to this :

 ObjectMapper mapper = new ObjectMapper();
 mapper.registerModule(new JavaTimeModule());
 mapper.disable( SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
Bacem W.
  • 92
  • 4