当前使用版本(必填,否则不予处理)

3.3.1

该问题是如何引起的?(确定最新版也有问题再提!!!)

JacksonTypeHandler List查询出来却是List get()该字段将会出现 java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Long 异常

重现步骤(如果有就写完整)

MyBatis-Plus JacksonTypeHandler List<Long>查询出来却是List<Integer> MyBatis-Plus JacksonTypeHandler List<Long>查询出来却是List<Integer>

报错信息

get()该字段将会出现 java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Long 异常

Comment From: wanghongenpin

MyBatis-Plus JacksonTypeHandler List<Long>查询出来却是List<Integer> 这里是否可以把泛型信息解析出来哪?

Comment From: miemieYaho

解析不了

Comment From: a296421914

遇到同样问题,下面属性解析出来是数据成了 List 类型 @TableField(typeHandler = JacksonTypeHandler.class) private List campaignIds;

Comment From: JsonSong89

@miemieYaho
可否在TableField上增加一个字段提供在集合类型反序列化时的类型信息?

Comment From: kala888

一直无解么?

Comment From: HanHan666666

自己实现一个 TypeHandler 用于专门处理 List Long 类型的数据

import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.io.IOException;
import java.util.List;

/**
 * Jackson 实现 List< Long> 字段类型处理器
 *
 * @author hanhan666666
 * @since 2024-3-28
 */
@Slf4j
@MappedTypes({Object.class})
@MappedJdbcTypes(JdbcType.VARCHAR)
@AllArgsConstructor
@Component
public class ListLongJacksonHandler extends AbstractJsonTypeHandler<Object> {
    private final ObjectMapper objectMapper;
    @Override
    protected Object parse(String json) {
        try {
            return objectMapper.readValue(json, objectMapper.getTypeFactory().constructCollectionType(List.class, Long.class));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected String toJson(Object obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

}