-
Notifications
You must be signed in to change notification settings - Fork 463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] 对象toJSONString异常 #798
Comments
Configuration |
@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) |
https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson/2.0.15-SNAPSHOT/ |
是因为bean被spring代理导致的? |
https://github.com/alibaba/fastjson2/releases/tag/2.0.15 |
问题描述
简要描述您碰到的问题。
环境信息
请填写以下信息:
重现步骤
如何操作可以重现该问题:
填充数据:
Config(deviceCodes=[M, Q], barkPushUrl=https://baidu.com, barkPushToken=token, location=广东 深圳 南山区, storeNameWhiteList=[深圳湾])
针对于此类和数据,使用 JSON.toJSONString(config) 时抛出异常。(奇怪,通过直接new的方式创建这个对象就可以正常toJsonString,但是通过读配置文件,以注入的形式使用这个对象就会抛异常。)
复现代码
期待的正确结果
对您期望发生的结果进行清晰简洁的描述。
正常转为json string
相关日志输出
附加信息
奇怪,通过直接new的方式创建这个对象就可以正常toJsonString,但是通过读配置文件的形式,就导致这里抛异常。
The text was updated successfully, but these errors were encountered: