前言

使用fastjson2对接SpringBoot 3.x后,调用JSON.toJSONString(this, JSONWriter.Feature.PrettyFormat)后报错了,内容为:

java.lang.ArrayIndexOutOfBoundsException: Index 8192 out of bounds for length 8192
	at com.alibaba.fastjson2.JSONWriterUTF16.writeName8Raw(JSONWriterUTF16.java:1719)
	at com.alibaba.fastjson2.writer.OWG_27_19_MenuListVo.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeListValue(FieldWriterList.java:218)
	at com.alibaba.fastjson2.writer.OWG_27_19_MenuListVo.write(Unknown Source)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeListValue(FieldWriterList.java:218)
	at com.alibaba.fastjson2.writer.OWG_27_19_MenuListVo.write(Unknown Source)
	at com.alibaba.fastjson2.writer.ObjectWriterImplList.write(ObjectWriterImplList.java:364)
	at com.alibaba.fastjson2.writer.OWG_11_4_RespPageVo.write(Unknown Source)
	at com.alibaba.fastjson2.JSON.toJSONString(JSON.java:2976)
	at com.itdct.spfl.extend.JsonExtendKt.toJson(JsonExtend.kt:15)
	at com.itdct.spfl.extend.JsonExtendKt.toJson$default(JsonExtend.kt:13)
	at com.itdct.spfl.aspect.BaseControllerAspect.handleResponse(BaseControllerAspect.kt:103)
	at com.itdct.spfl.aspect.BaseControllerAspect.around(BaseControllerAspect.kt:73)

解决方法

升级fastjson2版本,我目前用的是2.0.48,升级到2.0.57后该问题就解决了。

如果项目中没有用Kotlin可以跳过下面的坑。

但是!这个版本的fastjson2需要需要2.0的Kotlin才行,以至于我的Kotlin版本也要跟着升级,需要从原来的1.9.25升级到2.1.20。

又由于SpringBoot3.4.2版本的spring-boot-dependencies中存在Kotlin版本,为1.9.25,以至于还需要在最外层的pom.xml中定义Kotlin版本,覆盖spring-boot-dependencies中的版本号才行。需要在properties添加:

            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <args>
                        <arg>-Xjsr305=strict</arg>
                    </args>
                    <compilerPlugins>
                        <plugin>spring</plugin>
                        <plugin>all-open</plugin>
                    </compilerPlugins>
                    <jvmTarget>1.8</jvmTarget>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.jetbrains.kotlin</groupId>
                        <artifactId>kotlin-maven-allopen</artifactId>
                        <version>${kotlin.version}</version>
                    </dependency>
                    <!--kotlin 生成无参构造函数-->
                    <dependency>
                        <groupId>org.jetbrains.kotlin</groupId>
                        <artifactId>kotlin-maven-noarg</artifactId>
                        <version>${kotlin.version}</version>
                    </dependency>
                </dependencies>
            </plugin>

重新定义kotlin-maven-plugin的版本号,才解决这个问题。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐