json字段自动映射到实体类

Json数据:
[{“flow”: 215462, “timeNode”: “2023-05-26 13:55:00”}]

表数据对象文件

@TableName(value="table_name", autoResultMap = true)
........
public class EquipmentFlowDO extends BaseDO {

    /**
     * id
     */
    @TableId
    private Long id;
    /**
     * 流量数据
     */
    @TableField(typeHandler = EquipmentFlowTypeHandler.class)
    private List<EquipmentFlowVO> data;

json映射的类

@Data
public class EquipmentFlowVO {

    @Schema(description = "时间节点")
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime timeNode;

    @Schema(description = "流量值")
    private Integer flow;
}

json类型转化

/**
 * @author fsg
 * @apiNote 类型转换器:所有点位数据
 * @date 2023年5月22日
 * @see com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler
 */
@MappedTypes({Object.class})
@MappedJdbcTypes({JdbcType.VARCHAR})
public class EquipmentFlowTypeHandler extends AbstractJsonTypeHandler<List<EquipmentFlowVO>> {

    private static final Logger log = LoggerFactory.getLogger(EquipmentFlowTypeHandler.class);
    private static final ObjectMapper mapper = new ObjectMapper();

    static {
        // 转换为格式化的json
        mapper.enable(SerializationFeature.INDENT_OUTPUT);

        // 如果json中有新增的字段并且是实体类类中不存在的,不报错
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        // 下面配置解决LocalDateTime序列化的问题
        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        JavaTimeModule javaTimeModule = new JavaTimeModule();

        //日期序列化
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        //日期反序列化
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        mapper.registerModule(javaTimeModule);
    }
    public EquipmentFlowTypeHandler(Class<?> type) {
        if (log.isTraceEnabled()) {
            log.trace("EquipmentFlowTypeHandler(" + type + ")");
        }
        Assert.notNull(type, "Type argument cannot be null");
    }

    @Override
    protected List<EquipmentFlowVO> parse(String json) {
        if (StrUtil.isEmpty(json)) {
            return new ArrayList<>();
        }
        try {
            return mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, EquipmentFlowVO.class));
        } catch (IOException e) {
            log.error("json parse err,json:{}", json, e);
            throw new RuntimeException(e);
        }
    }

    @Override
    protected String toJson(List<EquipmentFlowVO> list) {
        try {
            // 格式化时间
            return mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
                    .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
                    .registerModules(new JavaTimeModule())
                    .writeValueAsString(list);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值