解决大对象toString导致的OOM fastJson中的toString发生OOM异常

项目场景:

在项目中,如果碰到大对象(一般指100M)以上的对象,用不管fastjson还是fastjson2中的toString都会可能导致OOM的存在,所有对大对象而言,可以使用以下的工具类


依赖和代码

java 复制代码
<dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.12.0</version>
</dependency>
java 复制代码
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.json.JsonMapper;

public class BigObjToStrUtil {
    public static String bigObjToStr(Object obj) {
        JsonMapper jsonMapper = JsonMapper.builder()
                .build();
        String res = null;
        try {
            res = jsonMapper.writer()
                    .writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return res;
    }
}

原理:

就是利用的jsonMapper去实现的

相关推荐
Macbethad12 分钟前
自动化测试技术报告
开发语言·lua
38242782713 分钟前
python:输出JSON
前端·python·json
不会画画的画师17 分钟前
Go开发指南:io/ioutil包应用和迁移指南
开发语言·后端·golang
2503_9284115618 分钟前
12.22 wxml语法
开发语言·前端·javascript
AM越.18 分钟前
Java设计模式详解--装饰器设计模式(含uml图)
java·设计模式·uml
59803541535 分钟前
【java工具类】小数、整数转中文大写
android·java·开发语言
JIngJaneIL43 分钟前
基于java + vue个人博客系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
吃喝不愁霸王餐APP开发者1 小时前
Java后端服务在对接全国性霸王餐API时的多数据中心部署与就近调用策略
java·开发语言
从心归零1 小时前
springboot-jpa的批量更新方法
java·spring boot·spring
froginwe111 小时前
jQuery UI 实例
开发语言