Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams 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\"])

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();
                Hi. You should probably use a Jackson2ObjectMapperBuilderCustomizer bean instead as shown here: baeldung.com/spring-boot-customize-jackson-objectmapper. Please note LOCAL_DATETIME_SERIALIZER. BR
– Roar S.
                Sep 18, 2022 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, 2022 at 13:01
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);
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.