[alibaba/fastjson]JSON.toJSONString()在某些情况下会发生死循环,最终StackOverflowError

2024-09-02 540 views
6
package com.org.joint.domain.request.dto;
import com.alibaba.fastjson.JSON;
import com.org.joint.common.enums.UDIEnum;
import com.org.joint.domain.udi.util.Httpsendpost;
import lombok.Data;

import java.io.Serializable;

/**
 * @author August  1985381864@qq.com
 * @version 1.0
 * @description: 获取UDI信息
 * @date 2021/12/29 17:25
 */
@Data
public class GetSharingParam implements Serializable {

    private static final long serialVersionUID = 1536020347622643888L;
    /**
     * 接口调用凭据
     */
    private String accessToken;

    /**
     * 请求范围,1 按天请求,2 按月请求,3 全量请求
     * @see UDIEnum.GetSharingParamRequestType
     */
    private String requestType;

    /**
     * 当按照天请求数据时,必填,输入参数按照 yyyy-MM-dd,
     * 当按照月请求数据时,必填,输入参数按照 yyyy-MM,
     * 全量获取时,该参数 为空
     */
    private String rangeValue;

    /**
     * 请求分页数,初始请求时,从 1开始
     */
    private String currentPageNumber;
    public static final String currentPageNumberStart = "1";

    /**
     * 获取请求参数
     * @return
     */
    public String getParam() {
        String param = "params=" + JSON.toJSONString(this);
        return param;
    }

    public static void main(String[] args) {
        GetSharingParam getSharingParam = new GetSharingParam();
        getSharingParam.setCurrentPageNumber("1");
        getSharingParam.setRangeValue("20110117");
        getSharingParam.setRequestType("1");
        getSharingParam.setAccessToken("111");
        System.out.println(getSharingParam.getParam());

    }
}

version

<dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.58</version>
</dependency>

错误日志

Exception in thread "main" java.lang.StackOverflowError at com.alibaba.fastjson.serializer.SerialContext.(SerialContext.java:10) at com.alibaba.fastjson.serializer.JSONSerializer.setContext(JSONSerializer.java:117) at com.alibaba.fastjson.serializer.JSONSerializer.setContext(JSONSerializer.java:109) at com.alibaba.fastjson.serializer.ASMSerializer_1_GetSharingParam.write(Unknown Source) at com.alibaba.fastjson.serializer.JSONSerializer.write(JSONSerializer.java:285) at com.alibaba.fastjson.JSON.toJSONString(JSON.java:740) at com.alibaba.fastjson.JSON.toJSONString(JSON.java:678) at com.alibaba.fastjson.JSON.toJSONString(JSON.java:643) at com.org.joint.domain.request.dto.GetSharingParam.getParam(GetSharingParam.java:49) at com.alibaba.fastjson.serializer.ASMSerializer_1_GetSharingParam.write(Unknown Source) at com.alibaba.fastjson.serializer.JSONSerializer.write(JSONSerializer.java:285) at com.alibaba.fastjson.JSON.toJSONString(JSON.java:740) at com.alibaba.fastjson.JSON.toJSONString(JSON.java:678) at com.alibaba.fastjson.JSON.toJSONString(JSON.java:643) at com.org.joint.domain.request.dto.GetSharingParam.getParam(GetSharingParam.java:49) at com.alibaba.fastjson.serializer.ASMSerializer_1_GetSharingParam.write(Unknown Source) at com.alibaba.fastjson.serializer.JSONSerializer.write(JSONSerializer.java:285) at com.alibaba.fastjson.JSON.toJSONString(JSON.java:740) at com.alibaba.fastjson.JSON.toJSONString(JSON.java:678) at com.alibaba.fastjson.JSON.toJSONString(JSON.java:643) at com.org.joint.domain.request.dto.GetSharingParam.getParam(GetSharingParam.java:49) at com.alibaba.fastjson.serializer.ASMSerializer_1_GetSharingParam.write(Unknown Source) at com.alibaba.fastjson.serializer.JSONSerializer.write(JSONSerializer.java:285) at com.alibaba.fastjson.JSON.toJSONString(JSON.java:740) at com.alibaba.fastjson.JSON.toJSONString(JSON.java:678) at com.alibaba.fastjson.JSON.toJSONString(JSON.java:643) at com.org.joint.domain.request.dto.GetSharingParam.getParam(GetSharingParam.java:49) at com.alibaba.fastjson.serializer.ASMSerializer_1_GetSharingParam.write(Unknown Source) at com.alibaba.fastjson.serializer.JSONSerializer.write(JSONSerializer.java:285) at com.alibaba.fastjson.JSON.toJSONString(JSON.java:740) at com.alibaba.fastjson.JSON.toJSONString(JSON.java:678) at com.alibaba.fastjson.JSON.toJSONString(JSON.java:643) at com.org.joint.domain.request.dto.GetSharingParam.getParam(GetSharingParam.java:49) at com.alibaba.fastjson.serializer.ASMSerializer_1_GetSharingParam.write(Unknown Source) at com.alibaba.fastjson.serializer.JSONSerializer.write(JSONSerializer.java:285) at com.alibaba.fastjson.JSON.toJSONString(JSON.java:740) at com.alibaba.fastjson.JSON.toJSONString(JSON.java:678) at com.alibaba.fastjson.JSON.toJSONString(JSON.java:643)

回答

7

有两个地方导致你出问题: 1、不应该在这个对象中使用main方法测试 2、在getParam方法中使用this 你可以在序列化的时候直接使用JSON.toJSONString(),将会解决这个问题 保持pojo对象简单,不要做逻辑

3

fastjson序列化对象的时候会调用get开头的方法,把getParam改个名(比如obtainParam)可解决问题

zhanzhandev的建议是对的,你的用法有问题

8

您好,来信已收到,会尽快给您回复。

7

感谢大家,已解决