当前使用版本

3.4.3.3

该问题是如何引起的?

@Data
@TableName(autoResultMap = true)
public class User {

    private Long id;

    private String name;

    @TableField(typeHandler = FastjsonTypeHandler.class)
    private List<UserDto> data;

}
@Data
public class UserDto {

    private String username;
    private String truename;
    private String state;
    private BigDecimal balance;
    private Boolean enable;
    private String note;

}
    @GetMapping("user/{id}")
    public R<?> testUser(@PathVariable("id") Long id) {
        userService.getById(id).getData().forEach(userDto -> {
            System.out.println(userDto.getUsername());
        });
        return R.ok();
    }

代码中不能正确解析JSON对象,抛出错误

重现步骤

创建MySQL数据库表,执行以上编码

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for t_user
-- ----------------------------
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `name` varchar(63) DEFAULT NULL COMMENT '配置键',
  `data` json DEFAULT NULL COMMENT '配置值',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='测试';

-- ----------------------------
-- Records of t_user
-- ----------------------------
BEGIN;
INSERT INTO `t_user` VALUES (1, 'jackson', '[{\"state\": \"normal\", \"enable\": true, \"balance\": 10000, \"truename\": \"Zhang\", \"username\": \"Andy\"}, {\"state\": \"normal\", \"enable\": true, \"balance\": 10000, \"truename\": \"Lee\", \"username\": \"Roger\"}, {\"state\": \"normal\", \"enable\": true, \"balance\": 10000, \"truename\": \"Yang\", \"username\": \"Jalen\"}]');
COMMIT;

SET FOREIGN_KEY_CHECKS = 1;

报错信息

java.lang.ClassCastException: class com.alibaba.fastjson.JSONObject cannot be cast to class com.nexus.domain.dtos.UserDto (com.alibaba.fastjson.JSONObject and com.nexus.domain.dtos.UserDto are in unnamed module of loader 'app')

Comment From: miemieYaho

不支持动态泛型的字段

class UserDtoList extends ArrayList<UserDto>{}

@TableField(typeHandler = FastjsonTypeHandler.class)
private UserDtoList data;