使用
return (Map<?, ?>) objectInputStream.readObject();
获取 Map,而不会在 graalVM 本机映像中失败。
代码其实很简单。只需将两个文件复制粘贴到您的环境中即可重现。
private static Map<?, ?> getNormalizedMap(String encoded) {
try (var objectInputStream = new ObjectInputStream(new ByteArrayInputStream(Base64.getDecoder().decode(encoded)))) {
return (Map<?, ?>) objectInputStream.readObject(); // issue here !
} catch (IOException | ClassNotFoundException | IllegalArgumentException e) {
LOGGER.error("encoded={}", encoded, e);
return Map.of();
}
}
完整文件:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Base64;
import java.util.Map;
@RestController
@SpringBootApplication
public class GraalissueApplication {
public static void main(String[] args) {
SpringApplication.run(GraalissueApplication.class, args);
}
@GetMapping("/graalissue")
public String index() {
String base64encoded = "rO0ABXNyACVqYXZhLnV0aWwuQ29sbGVjdGlvbnMkVW5tb2RpZmlhYmxlTWFw8aWo/nT1B0ICAAFMAAFtdAAPTGphdmEvdXRpbC9NYXA7eHBzcgARamF2YS51dGlsLkhhc2hNYXAFB9rBwxZg0QMAAkYACmxvYWRGYWN0b3JJAAl0aHJlc2hvbGR4cD9AAAAAAADAdwgAAAEAAAAAKXQAFXN0YWdlLWV2ZW50cy1vc2NfdWktKnNyABFqYXZhLnV0aWwuSGFzaFNldLpEhZWWuLc0AwAAeHB3DAAAABA/QAAAAAAABnQACHVzZXIudWlkdAAHdXNlci5pZHQAD3VzZXIuZXh0ZXJuYWxJZHQACWRldmljZS5pZHQACGRldmljZUlkdAAGdXNlcklkeHQAIXN0YWdlLWV2ZW50cy13YnRfaW5mcmFzdHJ1Y3R1cmUtKnNxAH4ABncMAAAAED9AAAAAAAAGcQB+AAhxAH4ACXEAfgAKcQB+AAtxAH4ADHEAfgANeHQAFHN0YWdlLWV2ZW50cy1hYmh1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0ABxzdGFnZS1ldmVudHMtZGlzcGxheWRyaXZlci0qc3EAfgAGdwwAAAAQP0AAAAAAAAZxAH4ACHEAfgAJcQB+AApxAH4AC3EAfgAMcQB+AA14dAAZc3RhZ2UtZXZlbnRzLXNoYWRvd3BsYXktKnNxAH4ABncMAAAAED9AAAAAAAAGcQB+AAhxAH4ACXEAfgAKcQB+AAtxAH4ADHEAfgANeHQAF3N0YWdlLWV2ZW50cy1jaHJvbWF1aS0qc3EAfgAGdwwAAAAQP0AAAAAAAAZxAH4ACHEAfgAJcQB+AApxAH4AC3EAfgAMcQB+AA14dAAac3RhZ2UtZXZlbnRzLW52YXBwY2xpZW50LSpzcQB+AAZ3DAAAABA/QAAAAAAABnEAfgAIcQB+AAlxAH4ACnEAfgALcQB+AAxxAH4ADXh0AB1ldmVudHMtc3ctZ3hfY3Jhc2hwcm9jZXNzb3ItKnNxAH4ABncMAAAAED9AAAAAAAAGcQB+AAhxAH4ACXEAfgAKcQB+AAtxAH4ADHEAfgANeHQAFnN0YWdlLWV2ZW50cy1wYXJsbGF5LSpzcQB+AAZ3DAAAABA/QAAAAAAABnEAfgAIcQB+AAlxAH4ACnEAfgALcQB+AAxxAH4ADXh0ACJzdGFnZS1yZWNvbXByZXNzb3ItZ2ZlX2ZlZWRiYWNrcy0qc3EAfgAGdwwAAAAQP0AAAAAAAAZxAH4ACHEAfgAJcQB+AApxAH4AC3EAfgAMcQB+AA14dAAXc3RhZ2UtZXZlbnRzLW52Y2FudmFzLSpzcQB+AAZ3DAAAABA/QAAAAAAABnEAfgAIcQB+AAlxAH4ACnEAfgALcQB+AAxxAH4ADXh0AB1zdGFnZS1mZWVkYmFja3MtZGlhZ25vc3RpY3MtKnNxAH4ABncMAAAAED9AAAAAAAAGcQB+AAhxAH4ACXEAfgAKcQB+AAtxAH4ADHEAfgANeHg=";
Map map = getNormalizedMap(base64encoded);
return "It should be 41 here => " + map.size();
}
private static Map<?, ?> getNormalizedMap(final String encoded) {
try (var objectInputStream = new ObjectInputStream(new ByteArrayInputStream(Base64.getDecoder().decode(encoded)))) {
return (Map<?, ?>) objectInputStream.readObject();
} catch (IOException | ClassNotFoundException | IllegalArgumentException e) {
e.printStackTrace();
return Map.of();
}
}
}
pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>graalissue</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>graalissue</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>22</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<env>
<BP_JVM_VERSION>22</BP_JVM_VERSION>
<BP_NATIVE_IMAGE_BUILD_ARGUMENTS>-H:-AddAllFileSystemProviders</BP_NATIVE_IMAGE_BUILD_ARGUMENTS>
</env>
</image>
</configuration>
</plugin>
</plugins>
</build>
</project>
return (Map<?, ?>) objectInputStream.readObject();
将返回正确的地图。对于这个问题,预期的结果应该是
curl http://localhost:8080/graalissue
对于本机和非本机图像,我希望看到“这里应该是 41 => 41”。
mvn -Pnative spring-boot:build-image
这将创建 graalvm 本机映像run docker tag docker.io/library/graalissue:0.0.1-SNAPSHOT graalissue:latest
docker run -p 8080:8080 graalissue:latest
curl http://localhost:8080/graalissue
输出大小将为 0。两者都失败了
java.util.Collections$UnmodifiableMap
添加到 serialization-config.json
java.util.Collections$UnmodifiableMap
添加到 serialization-config.json
(我什至不确定是否正确),我会得到 java.io.InvalidClassException: java.util.HashMap; local class incompatible: stream classdesc
如果我没有向
serialization-config.json
添加任何内容,则会出错,并且堆栈跟踪会要求我将 java.util.Collections$UnmodifiableMap
添加到 serialization-config.json
如果我添加它,我会得到这个堆栈跟踪:
java.io.InvalidClassException: java.util.HashMap; local class incompatible: stream classdesc serialVersionUID = 362498820763181265, local class serialVersionUID = -3563561681480877083
at [email protected]/java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:598)
at [email protected]/java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:2063)
at [email protected]/java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1912)
at [email protected]/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2237)
at [email protected]/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1747)
at [email protected]/java.io.ObjectInputStream$FieldValues.<init>(ObjectInputStream.java:2603)
at [email protected]/java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2454)
at [email protected]/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2269)
at [email protected]/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1747)
at [email protected]/java.io.ObjectInputStream.readObject(ObjectInputStream.java:525)
at [email protected]/java.io.ObjectInputStream.readObject(ObjectInputStream.java:483)
如何解决这个问题?
如何在 graalVM 原生镜像中正确地
return (Map<?, ?>) objectInputStream.readObject();
,因为非原生镜像中的代码根本没有问题?
我认为您需要向 AOT 编译器提示您正在序列化的类型,因为序列化需要反射才能工作。据我所知,您可以通过
@RegisterReflectionForBinding
指定(请参阅 docs)。引用文档:
@RegisterReflectionForBinding 是 @Reflective 的特化, 注册序列化任意类型的需要。典型用例 是容器无法推断的 DTO 的使用,例如使用 方法体内的 Web 客户端。
我还没有测试过这个,但我会尝试将注释添加为:
@RegisterReflectionForBinding(Map.class)
private static Map<?, ?> getNormalizedMap(final String encoded) {
try (var objectInputStream = new ObjectInputStream(new ByteArrayInputStream(Base64.getDecoder().decode(encoded)))) {
return (Map<?, ?>) objectInputStream.readObject();
} catch (IOException | ClassNotFoundException | IllegalArgumentException e) {
e.printStackTrace();
return Map.of();
}
}