alibaba / fastjson

FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
https://github.com/alibaba/fastjson2/wiki/fastjson_1_upgrade_cn
Apache License 2.0
25.75k stars 6.5k forks source link

实体由两个类加载器加载时,调用序列化方法提示NoClassDefFoundError #4404

Open ccc910283013 opened 1 year ago

ccc910283013 commented 1 year ago

实体对象

public class QueryStatisticRequest implements AbstractRequest { //这个枚举是来源于另一个jar文件 private SubTypeEnum subType; }

打包后调用 JSON.toJSONString(new QueryStatisticRequest()) 提示NoClassDefFoundError:Lxxxxxx

后调试发现SubTypeEnum 来源于launchclassloader 而QueryStatisticRequest 来源于appclassloader

最后通过修改打包方式解决了这个问题, 好奇这是fastjson的bug吗?

原打包方式:


<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>11</source>
                <target>11</target>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>${lombok.version}</version>
                    </path>
                    <!-- This is needed when using Lombok 1.18.16 and above -->
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok-mapstruct-binding</artifactId>
                        <version>0.2.0</version>
                    </path>
                    <!-- Mapstruct should follow the lombok path(s) -->
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>${org.mapstruct.version}</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>

        <!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.0.2</version>
            <configuration>
                <archive>
                    <!-- 生成的jar中,不要包含pom.xml和pom.properties这两个文件 -->
                    <addMavenDescriptor>false</addMavenDescriptor>
                    <!-- 给清单文件添加键值对(配置文件外置) -->
                    <manifestEntries>
                        <Class-Path>configs/</Class-Path>
                    </manifestEntries>
                    <!-- 清单文件 -->
                    <manifest>
                        <!--应用的main class-->
                        <mainClass>cn.jinka.gcdp.metacenter.MetaCenterApplication</mainClass>
                        <!--是否要把第三方jar添加到classpath中-->
                        <addClasspath>true</addClasspath>
                        <!--生成的manifest中classpath的前缀,因为要把第三方jar放到lib目录下,所以classpath的前缀是lib/-->
                        <classpathPrefix>lib/</classpathPrefix>
                    </manifest>
                </archive>
                <!--过滤掉不希望包含在jar中的文件-->
                <excludes>
                    <exclude>mapper/**</exclude>
                    <exclude>*.yml</exclude>
                    <exclude>*.xml</exclude>
                    <exclude>*.txt</exclude>
                </excludes>
            </configuration>
        </plugin>
        <!-- 拷贝依赖的jar包到lib目录 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.0.2</version>
            <executions>
                <execution>
                    <id>copy</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                        <outputDirectory>${project.build.directory}/lib/</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- 解决资源文件的编码问题 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.0.2</version>
            <configuration>
                <encoding>UTF-8</encoding>
            </configuration>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/configs</outputDirectory>
                        <resources>
                            <resource>
                                <directory>${basedir}/src/main/resources</directory>
                                <filtering>true</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <!-- 自定义打zip包 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <descriptors>
                    <descriptor>src/main/assembly/assembly.xml</descriptor>
                </descriptors>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>install</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

>新的打包方式
org.apache.maven.plugins maven-compiler-plugin 3.8.1 11 11 org.projectlombok lombok ${lombok.version} org.projectlombok lombok-mapstruct-binding 0.2.0 org.mapstruct mapstruct-processor 1.5.2.Final org.springframework.boot spring-boot-maven-plugin true org.projectlombok lombok
ccc910283013 commented 1 year ago

使用版本是1.2.70