使用PowerQuery返回实体集合的OData函数失败

时间:2016-02-23 08:39:09

标签: java json olingo

在我正在开发的Odata服务上使用PowerQuery时出现此错误:

  

编写JSON响应时,必须指定用户模型   实体集和实体类型必须传递给   ODataMessageWriter.CreateODataEntryWriter方法或   必须在ODataEntry或上设置ODataFeedAndEntrySerializationInfo   正在编写的ODataFeed。

从PowerQuery调用返回实体集合的绑定函数时会发生这种情况。从Web浏览器调用时,响应为(JSON格式):

{
    "@odata.context": "http://localhost:8080/ODataPrototype/ODataPrototype.svc/$metadata#Collection(Demo.ODataPrototype.Count)",
    "value": [
        {
            "RowCount": 1
        },
        {
            "RowCount": 2
        },
        {
            "RowCount": 3
        },
        {
            "RowCount": 4
        }
    ]
}

我使用Olingo V4库。我的元数据的精简版本将是:

<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
<edmx:DataServices>
<Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="Demo.ODataPrototype">
<EntityType Name="Instance">
<Key>
<PropertyRef Name="Id"/>
</Key>
<Property Name="Id" Type="Edm.Int32"/>
<Property Name="Name" Type="Edm.String"/>
<Property Name="Description" Type="Edm.String"/>
<Property Name="Tag" Type="Edm.String"/>
<Property Name="Xid" Type="Edm.Int64"/>
<Property Name="Properties" Type="Collection(Demo.ODataPrototype.Property)"/>
</EntityType>
<EntityType Name="Count">
<Property Name="RowCount" Type="Edm.Int32"/>
</EntityType>
<ComplexType Name="Property">
<Property Name="Name" Type="Edm.String"/>
<Property Name="Value" Type="Edm.String"/>
</ComplexType>
<Function Name="GetData" EntitySetPath="Instance/Demo.ODataPrototype.Count" IsBound="true">
<Parameter Name="Instance" Type="Demo.ODataPrototype.Instance"/>
<Parameter Name="From" Type="Edm.DateTimeOffset"/>
<Parameter Name="To" Type="Edm.DateTimeOffset"/>
<ReturnType Type="Collection(Demo.ODataPrototype.Count)"/>
</Function>
<EntityContainer Name="Container">
<EntitySet Name="Instances" EntityType="Demo.ODataPrototype.Instance">
</EntitySet>
<EntitySet Name="Count" EntityType="Demo.ODataPrototype.Count"/>
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>

有谁知道我错过了什么?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我的JSON响应中的上下文将结果定义为IVserver.ODataPrototype.Count类型的集合:

"@odata.context": "http://localhost:8080/ODataPrototype/ODataPrototype.svc/$metadata#Collection(IVserver.ODataPrototype.Count)"

但是PowerQuery需要一个实体集:

"@odata.context": "http://localhost:8080/ODataPrototype/ODataPrototype.svc/$metadata#Count"

EntitySet&#34; Count&#34;必须在EntityContainer中声明。

要在@ odata.context中获取EntitySet,必须在构建ContextURL时设置实体集。例如:

final ContextURL contextURL = ContextURL.with().entitySet(responseEdmEntitySet).selectList(selectList).serviceRoot(baseURI).build();

并且,方法 asCollection() type()必须