Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[BUG] 对象toJSONString异常 #798

Closed
MoshiCoCo opened this issue Sep 26, 2022 · 5 comments
Closed

[BUG] 对象toJSONString异常 #798

MoshiCoCo opened this issue Sep 26, 2022 · 5 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@MoshiCoCo
Copy link
Contributor

MoshiCoCo commented Sep 26, 2022

问题描述

简要描述您碰到的问题。

环境信息

请填写以下信息:

  • OS信息: win10
  • JDK信息:1.8.0.301
  • 版本信息:2.0.14

重现步骤

如何操作可以重现该问题:

@Data
@Configuration
@ConfigurationProperties(prefix = "config")
public class Config {
    public String[] deviceCodes;
    public String barkPushUrl;
    public String barkPushToken;
    public String location;
    public List<String> storeNameWhiteList;
}

填充数据:Config(deviceCodes=[M, Q], barkPushUrl=https://baidu.com, barkPushToken=token, location=广东 深圳 南山区, storeNameWhiteList=[深圳湾])

针对于此类和数据,使用 JSON.toJSONString(config) 时抛出异常。(奇怪,通过直接new的方式创建这个对象就可以正常toJsonString,但是通过读配置文件,以注入的形式使用这个对象就会抛异常。)

复现代码

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

import com.alibaba.fastjson2.JSON;

@SpringBootTest
@ActiveProfiles("test")
public class IssuesTest {
    @Autowired
    Config configs;

    @Test
    public void test() {
        Config config = new Config();
        assertEquals("{}", JSON.toJSONString(config));

        System.out.println(configs.toString());
        System.out.println(JSON.toJSONString(configs));
    }
}

期待的正确结果

对您期望发生的结果进行清晰简洁的描述。

正常转为json string

相关日志输出

java.lang.StackOverflowError
	at sun.reflect.GeneratedMethodAccessor21.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.alibaba.fastjson2.writer.FieldWriterObjectMethod.getFieldValue(FieldWriterObjectMethod.java:27)
	at com.alibaba.fastjson2.writer.FieldWriterObject.write(FieldWriterObject.java:143)
	at com.alibaba.fastjson2.writer.ObjectWriterAdapter.write(ObjectWriterAdapter.java:293)
	at com.alibaba.fastjson2.writer.ObjectWriter_39.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriter_40.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriter_39.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriter_40.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriter_39.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriter_40.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriter_39.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriter_40.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriter_39.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriter_40.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriter_39.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriter_40.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriter_39.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriter_40.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriter_39.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriter_40.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriter_39.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriter_40.write(Unknown Source)
	-----省略N行相同输出

附加信息

奇怪,通过直接new的方式创建这个对象就可以正常toJsonString,但是通过读配置文件的形式,就导致这里抛异常。

@MoshiCoCo MoshiCoCo added the bug Something isn't working label Sep 26, 2022
@wenshao
Copy link
Member

wenshao commented Sep 26, 2022

Configuration
ConfigurationProperties
这两个是什么package下的?需要依赖什么类库?

wenshao added a commit that referenced this issue Sep 26, 2022
@MoshiCoCo
Copy link
Contributor Author

MoshiCoCo commented Sep 26, 2022

Configuration ConfigurationProperties 这两个是什么package下的?需要依赖什么类库?

@wenshao ,我在这里补充复现代码和环境依赖供您使用。

测试类代码

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

import com.alibaba.fastjson2.JSON;


@SpringBootTest
@ActiveProfiles("test")
public class IssuesTest {
    @Autowired
    Config configs;

    @Test
    public void test() {
        //new对象时,可以正常toJSONString
        Config config = new Config();
        config.setLocation("广东");
        assertEquals("{\"location\":\"广东\"}", JSON.toJSONString(config));

        // 使用@Autowired 注入时,可以正常调用类toString打印属性,但是不能toJSONString
        System.out.println(configs.toString());
        System.out.println(JSON.toJSONString(configs));
    }
}

springboot application.yml 关于此对象的配置文件片段

config:
  deviceCodes:
    - 'M'
    - 'Q'
  barkPushUrl: https://baidu.com
  barkPushToken: token
  location: 广东 深圳 南山区
  storeNameWhiteList:
    - '深圳湾'

使用的springboot 依赖版本

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.7.4</version>
        </dependency>

        <!--         https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.14</version>
        </dependency>

异常的具体日志输出(晚上使用了M1 aarch64 zulu jdk17 得到了一个和上午不一样的异常)

2022-09-26 23:17:56.202  INFO 72171 --- [           main] service.impl.IssuesTest  : Started IssuesTest in 2.821 seconds (JVM running for 3.821)
Config(deviceCodes=[M, Q], barkPushUrl=https://baidu.com, barkPushToken=token, location=广东 深圳 南山区, storeNameWhiteList=[深圳湾])

com.alibaba.fastjson2.JSONException: invoke getter method error, definedPackages

	at com.alibaba.fastjson2.writer.FieldWriterObjectMethod.getFieldValue(FieldWriterObjectMethod.java:29)
	at com.alibaba.fastjson2.writer.FieldWriterObject.write(FieldWriterObject.java:143)
	at com.alibaba.fastjson2.writer.ObjectWriterAdapter.write(ObjectWriterAdapter.java:293)
	at com.alibaba.fastjson2.writer.ObjectWriter_11.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterObject.write(FieldWriterObject.java:252)
	at com.alibaba.fastjson2.writer.ObjectWriterAdapter.write(ObjectWriterAdapter.java:293)
	at com.alibaba.fastjson2.writer.ObjectWriter_11.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterObject.write(FieldWriterObject.java:252)
	at com.alibaba.fastjson2.writer.ObjectWriterAdapter.write(ObjectWriterAdapter.java:293)
	at com.alibaba.fastjson2.writer.ObjectWriter_11.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterObject.write(FieldWriterObject.java:252)
	at com.alibaba.fastjson2.writer.ObjectWriterAdapter.write(ObjectWriterAdapter.java:293)
	at com.alibaba.fastjson2.writer.ObjectWriter_11.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterObject.write(FieldWriterObject.java:252)
	at com.alibaba.fastjson2.writer.ObjectWriterAdapter.write(ObjectWriterAdapter.java:293)
	at com.alibaba.fastjson2.writer.ObjectWriter_11.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterObject.write(FieldWriterObject.java:252)
	at com.alibaba.fastjson2.writer.ObjectWriterAdapter.write(ObjectWriterAdapter.java:293)
	at com.alibaba.fastjson2.writer.ObjectWriter_11.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterObject.write(FieldWriterObject.java:252)
	at com.alibaba.fastjson2.writer.ObjectWriterAdapter.write(ObjectWriterAdapter.java:293)

使用aarch zulu jdk8 的异常日志为

2022-09-26 23:22:53.532  INFO 74652 --- [           main] service.impl.IssuesTest  : Started IssuesTest in 4.113 seconds (JVM running for 5.875)
Config(deviceCodes=[M, Q], barkPushUrl=https://baidu.com, barkPushToken=token, location=广东 深圳 南山区, storeNameWhiteList=[深圳湾])

java.lang.StackOverflowError
	at com.alibaba.fastjson2.writer.ObjectWriterAdapter.write(ObjectWriterAdapter.java:239)
	at com.alibaba.fastjson2.writer.ObjectWriter_31.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriter_30.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterObject.write(FieldWriterObject.java:252)
	at com.alibaba.fastjson2.writer.ObjectWriterAdapter.write(ObjectWriterAdapter.java:293)
	at com.alibaba.fastjson2.writer.ObjectWriterImplCollection.write(ObjectWriterImplCollection.java:171)
	at com.alibaba.fastjson2.writer.ObjectWriter_16.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriter_33.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriterImplMap.write(ObjectWriterImplMap.java:459)
	at com.alibaba.fastjson2.writer.ObjectWriter_32.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriterImplCollection.write(ObjectWriterImplCollection.java:171)
	at com.alibaba.fastjson2.writer.ObjectWriter_16.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriter_33.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriterImplMap.write(ObjectWriterImplMap.java:459)
	at com.alibaba.fastjson2.writer.ObjectWriter_32.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriterImplCollection.write(ObjectWriterImplCollection.java:171)
	at com.alibaba.fastjson2.writer.ObjectWriter_16.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriter_33.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriterImplMap.write(ObjectWriterImplMap.java:459)
	at com.alibaba.fastjson2.writer.ObjectWriter_32.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriterImplCollection.write(ObjectWriterImplCollection.java:171)
	at com.alibaba.fastjson2.writer.ObjectWriter_16.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriter_33.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriterImplMap.write(ObjectWriterImplMap.java:459)
	at com.alibaba.fastjson2.writer.ObjectWriter_32.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriterImplCollection.write(ObjectWriterImplCollection.java:171)
	at com.alibaba.fastjson2.writer.ObjectWriter_16.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriter_33.write(Unknown Source)

@wenshao wenshao added this to the 2.0.15 milestone Sep 27, 2022
@wenshao
Copy link
Member

wenshao commented Sep 27, 2022

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson/2.0.15-SNAPSHOT/
问题已修复,请帮忙用2.0.15-SNAPSHOT版本验证,2.0.15版本预计在10月7日前发布

@MoshiCoCo
Copy link
Contributor Author

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson/2.0.15-SNAPSHOT/ 问题已修复,请帮忙用2.0.15-SNAPSHOT版本验证,2.0.15版本预计在10月7日前发布

是因为bean被spring代理导致的?

@wenshao
Copy link
Member

wenshao commented Oct 5, 2022

https://github.com/alibaba/fastjson2/releases/tag/2.0.15
2.0.15版本已发布,请用新版本

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

2 participants