将 objects 从 WebAPI 返回到 Flex

Returning objects from WebAPI to Flex

我是 Flex 新手,我正在编写一个 MXML 应用程序,它将从 WebAPI 服务接收信息并显示它。

到目前为止,我已经设法用一个简单的字符串实现了这一点,但是如果我尝试 return 一个复杂的 object,它 return 为空。

这是我的代码,它是一个非常简单的形式:

<?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"
    xmlns:acrs_surveys="services.acrs_surveys.*"
    xmlns:valueObjects="valueObjects.*"
    minWidth="955" minHeight="600">
    <s:layout>
        <s:VerticalLayout/>
        </s:layout>
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.rpc.events.ResultEvent;

            protected function button_clickHandler(event:MouseEvent):void
            {
                GetSurveyResult.token = acrs_surveys.GetSurvey(itemidTextInput.text);
            }

            protected function button2_clickHandler(event:MouseEvent):void
            {
                survey.SurveyId = surveyIdTextInput.text;
                survey.Title = titleTextInput.text;
            }

            protected function assignSurveyResult(event:ResultEvent):void
            {
                survey = event.result as Survey;
            }
        ]]>
    </fx:Script>
    <fx:Declarations>
        <s:CallResponder id="GetSurveyResult" result="assignSurveyResult(event)"/>
        <valueObjects:Survey id="survey"/>
        <acrs_surveys:Acrs_surveys id="acrs_surveys" fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)" showBusyCursor="true" />
    </fx:Declarations>
    <s:Form defaultButton="{button}">
        <s:FormItem label="Itemid">
            <s:TextInput id="itemidTextInput"/>
        </s:FormItem>
        <s:Button id="button" label="GetSurvey" click="button_clickHandler(event)"/>
    </s:Form>
    <s:Form defaultButton="{button2}">
        <s:FormHeading label="Survey"/>
        <s:FormItem label="Questions">
            <s:Label id="QuestionsLabel" text="Questions_type[]"/>
        </s:FormItem>
        <s:FormItem label="SurveyId">
            <s:TextInput id="surveyIdTextInput" text="{survey.SurveyId}"/>
        </s:FormItem>
        <s:FormItem label="Title">
            <s:TextInput id="titleTextInput" text="{survey.Title}"/>
        </s:FormItem>
        <s:Button id="button2" label="Submit" click="button2_clickHandler(event)"/>
    </s:Form>
</s:Application>

代码主要由 Flash Builder 的向导生成。 "Survey" class 由生成服务调用向导生成,包含三个属性 - GUID、标题和问题列表 object,它们也由同一个向导。

当我运行代码时,没有发生错误,并且根据网络监视器,调用成功并且数据被returned。但是,当我破解 assignSurveyResult 中的代码并检查调查 object 时,结果为空。

如果我更改代码以使用 return 仅将调查标题作为字符串的 WebAPI 调用,它可以正常工作。似乎只有 object 行不通。

我做错了什么?

通过Flex源代码的追踪,我找到了解决方案。事实证明,Flash Builder 生成的客户端代理代码已将自己设置为将我的 XML 数据反序列化为 JSON,结果反序列化后的数据显示为“http://www.w3.org/2001/XMLSchema-instance”,这无法转换为对象。

使用“生成服务调用”向导时,会生成一个包,在我的例子中称为 "services.acrs_surveys"。在这个包中有一个“_Super_Acrs_surveys()”class,它在头部声明了两个序列化器:

private static var serializer0:JSONSerializationFilter = new JSONSerializationFilter();
private static var serializer1:XMLSerializationFilter = new XMLSerializationFilter();

此 class 的构造函数设置将使用哪个序列化程序:

 operation = new mx.rpc.http.Operation(null, "GetSurvey");
 operation.url = "Get/{itemid}";
 operation.method = "GET";
 argsArray = new Array("itemid");
 operation.argumentNames = argsArray;         
 operation.serializationFilter = serializer0;
 operation.properties = new Object();
 operation.properties["urlParamNames"] = ["itemid"];
 operation.resultType = valueObjects.Survey;
 operations.push(operation);

请注意,operation.serializationFilter 设置为 serializer0,这是 JSON 序列化程序,即使此操作的数据实际上是 XML。向导似乎没有正确检测到数据类型,因此出现了问题。

我将序列化程序更改为 serializer1,现在我的代码可以运行了。

有趣的是,在 XML 中只返回一个字符串的操作被正确检测到,这就是它起作用的原因,正如我的问题中提到的。