使用 BlazeDS 和 Flash Builder 从 java POJO 读取远程对象时出现空值

发布于 2025-01-08 09:38:42 字数 3609 浏览 0 评论 0原文

当我运行以下文件时,我没有收到任何错误,但从来自以下 Trace() 语句的 Java POJO 对象返回的数据是:

[object ComputerInfo]
null
[object ComputerInfo]
null

我不知道如何调试它。我尝试调整采用的代码 从这里

我的客户文件如下。我的 mxml 文件是:

<?xml version="1.0" encoding="utf-8"?>
<s:Application 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx">

    <fx:Script><![CDATA[
        import mx.controls.Alert;
        import mx.rpc.events.ResultEvent;
        import mx.rpc.events.FaultEvent;

        private var reqId1:int = 0;

        public var dataReadFromDB:ComputerInfo = new ComputerInfo;

        private function readFaultHandler( event:FaultEvent ):void 
        {
            Alert.show( event.fault.faultString, "Error reading data" ); 
        }

        private function readResultHandler( event:ResultEvent ):void
        { 
            dataReadFromDB.javaVersion = event.result.javaVersion; 
            dataReadFromDB.javaVendor = event.result.javaVendor; 
            dataReadFromDB.os = event.result.os; 
            dataReadFromDB.osVersion = event.result.osVersion; 
            dataReadFromDB.requestId = event.result.requestId; 
            trace(dataReadFromDB);
            trace(dataReadFromDB.javaVersion); 
            trace(event.result);
            trace(event.result.javaVersion);
        } 

    ]]></fx:Script>

    <fx:Declarations> 
        <mx:RemoteObject 
            id="ro" 
            destination="myDestination"
            showBusyCursor="true">
            <mx:method name="readData" 
                      result="readResultHandler(event);"  
                      fault="readFaultHandler(event);"/>
        </mx:RemoteObject>
    </fx:Declarations>

    <mx:Panel width="476" height="281" 
              layout="absolute" title="BlazeDS Example" 
              cornerRadius="0" backgroundColor="#ffffff">
        <s:Label x="46" y="59" text="Click to Read Data" />
        <s:Button x="200" y="59"  label="Read Now"   click="ro.readData( reqId1++ )"/>
    </mx:Panel> 
</s:Application>

ComputerInfo.as 文件:

package {
    [RemoteClass(alias="ComputerInfo")]
     public class ComputerInfo {
        public var javaVersion:String;
        public var javaVendor:String;
        public var os:String;
        public var osVersion:String;
        public var requestId:String;
    }
}

我的 java 文件是:

public class MyClass {
    public ComputerInfo readData( String requestId ) {
            ComputerInfo computerInfo = new ComputerInfo();
            computerInfo.javaVersion = "java.vm.version";
            computerInfo.javaVendor = "java.vm.vendor";
            computerInfo.os = "osname";
            computerInfo.osVersion = "os.version";
            computerInfo.requestId = "requestId";
            return computerInfo;
    }
}

和一个名为 ComputerInfo.java 的单独文件:

public class ComputerInfo {
    String javaVersion;
    String javaVendor;
    String os;
    String osVersion;
    String requestId;
}

内部 remoting-config.xml:

<destination id="myDestination">
            <properties>
                    <source>myClass</source>
            </properties>
</destination>

I'm not getting any errors when I run the following files, but the data returned from the Java POJO object coming from the trace() statements below is:

[object ComputerInfo]
null
[object ComputerInfo]
null

and I'm out of ideas how to debug this. I've tried to adapt the code taken from here.

My client files are as follows. My mxml file is:

<?xml version="1.0" encoding="utf-8"?>
<s:Application 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx">

    <fx:Script><![CDATA[
        import mx.controls.Alert;
        import mx.rpc.events.ResultEvent;
        import mx.rpc.events.FaultEvent;

        private var reqId1:int = 0;

        public var dataReadFromDB:ComputerInfo = new ComputerInfo;

        private function readFaultHandler( event:FaultEvent ):void 
        {
            Alert.show( event.fault.faultString, "Error reading data" ); 
        }

        private function readResultHandler( event:ResultEvent ):void
        { 
            dataReadFromDB.javaVersion = event.result.javaVersion; 
            dataReadFromDB.javaVendor = event.result.javaVendor; 
            dataReadFromDB.os = event.result.os; 
            dataReadFromDB.osVersion = event.result.osVersion; 
            dataReadFromDB.requestId = event.result.requestId; 
            trace(dataReadFromDB);
            trace(dataReadFromDB.javaVersion); 
            trace(event.result);
            trace(event.result.javaVersion);
        } 

    ]]></fx:Script>

    <fx:Declarations> 
        <mx:RemoteObject 
            id="ro" 
            destination="myDestination"
            showBusyCursor="true">
            <mx:method name="readData" 
                      result="readResultHandler(event);"  
                      fault="readFaultHandler(event);"/>
        </mx:RemoteObject>
    </fx:Declarations>

    <mx:Panel width="476" height="281" 
              layout="absolute" title="BlazeDS Example" 
              cornerRadius="0" backgroundColor="#ffffff">
        <s:Label x="46" y="59" text="Click to Read Data" />
        <s:Button x="200" y="59"  label="Read Now"   click="ro.readData( reqId1++ )"/>
    </mx:Panel> 
</s:Application>

ComputerInfo.as file:

package {
    [RemoteClass(alias="ComputerInfo")]
     public class ComputerInfo {
        public var javaVersion:String;
        public var javaVendor:String;
        public var os:String;
        public var osVersion:String;
        public var requestId:String;
    }
}

My java files are:

public class MyClass {
    public ComputerInfo readData( String requestId ) {
            ComputerInfo computerInfo = new ComputerInfo();
            computerInfo.javaVersion = "java.vm.version";
            computerInfo.javaVendor = "java.vm.vendor";
            computerInfo.os = "osname";
            computerInfo.osVersion = "os.version";
            computerInfo.requestId = "requestId";
            return computerInfo;
    }
}

and a separate file called ComputerInfo.java:

public class ComputerInfo {
    String javaVersion;
    String javaVendor;
    String os;
    String osVersion;
    String requestId;
}

Inside remoting-config.xml:

<destination id="myDestination">
            <properties>
                    <source>myClass</source>
            </properties>
</destination>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

池予 2025-01-15 09:38:42

您似乎在数据传输对象 (ComputerInfo.as) 中缺少 [RemoteClass] 元标记:(

package {
    [RemoteClass(alias="com.myjavapackage.ComputerInfo")]
    public class ComputerInfo { ... }
}

您必须将“com.myjavapackage”替换为适当的值;您的代码示例中未提及该包)

如果缺少标签,Flex 将无法将数据反序列化为适当的类型,并且您会得到一个“ObjectProxy”类型的对象。

您还可以删除自己对结果值的复制 - 如果所有内容均已正确注册,则 event.result 将已经是 ComputerInfo 类型。

编辑

另外,Java 类中的字段变量应该是 public,或者应该有 public getter 和 setter 方法。

有关详细信息,请参阅 Adobe 文档。相关部分是“显式映射 ActionScript 和 Java 对象”。

You seem to be missing the [RemoteClass] meta tags in your data transfer object (ComputerInfo.as):

package {
    [RemoteClass(alias="com.myjavapackage.ComputerInfo")]
    public class ComputerInfo { ... }
}

(where you'd have to replace "com.myjavapackage" with the appropriate values; the package was not mentioned in your code samples)

If the tags are missing, Flex won't be able to deserialize the data into the appropriate type, and you get an object of type "ObjectProxy" instead.

You can also remove your own copying of the result values - event.result will already be of type ComputerInfo, if everything is registered correctly.

EDIT

Also, your the field variables in your Java class should be public, or there should be public getter and setter methods.

For more info, see the Adobe Doc. The relevant section is "Explicitly mapping ActionScript and Java objects".

太阳公公是暖光 2025-01-15 09:38:42

虽然这不是您的 BlazeDS 问题的直接答案(weltraumpirat 已经很好地回答了),您也可以考虑使用 GraniteDS 及其代码生成工具(请参阅文档 此处):您将不再需要手动编写 AS3 类,并且所有 Java 字段(包括私有/受保护字段)都将被序列化(尽管不透明 Java 字段,即声明为私有且没有 getter/setter 的字段,在生成的 AS3 类中将保持不透明)。

While this is not a direct answer to your BlazeDS question (weltraumpirat has already answered it quite well), you could also consider using GraniteDS and its code generation tools (see documentation here): you wouldn't have anymore to write by hands your AS3 classes and all your Java fields, including private/protected ones, would be serialized (though an opaque Java field, ie. declared private with no getter/setter, would remain opaque in the generated AS3 class).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文