更新时间:2023-12-05 14:44:04
Nashorn对于Javascript数组具有类似NativeArray
的类,因为Javascript数组未绑定到像Java中的String[]
这样的元素类型.因此,Nashorn创建NativeArray
的实例.显然,流程引擎无法存储NativeArray
的实例,因为它没有实现java.io.Serializable
,并且也无法通过JSON和XML序列化器进行序列化.实际上,JSON序列化程序会尝试这样做,但是会抛出您看到的异常.
Nashorn has classes like NativeArray
for Javascript arrays because a Javascript array is not tied to an element type like for example String[]
in Java. Thus, Nashorn creates an instance of NativeArray
. Apparently, the process engine is not able to store instances of NativeArray
since it does not implement java.io.Serializable
and is not serializable by the JSON and XML serializer either. In fact, the JSON serializer attempts to do so but throws the exception you see.
你可以做
execution.setVariable("arr1", Java.to(merged, "java.lang.Object[]"));
将NativeArray
转换为Java Object[]
.如果要从Java代码访问该数组,则可以使用更具体类型的数组.来源: Nashorn文档
to convert the NativeArray
to a Java Object[]
. If you want to access the array from Java code, you could use a more specifically typed array. Source: Nashorn documentation
注意:
对于 JDK 8版本> = 1.8u40 ,移交的类型不是NativeArray
,而是包装了NativeArray
的ScriptObjectMirror
实例(请参见
For JDK 8 versions >= 1.8u40, the type handed over is not NativeArray
but an instance of ScriptObjectMirror
that wraps a NativeArray
(see this question for details). Apparently, the same code can be used to solve the issue.