Issue
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();
}
Solution
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);
Answered By - Bacem W.
Answer Checked By - Robin (JavaFixing Admin)