22

是否可以获取 JSON 格式的 OData 服务的元数据?

当我尝试使用format=json时,它不起作用。这是我尝试过的:

http://odata.informea.org/services/odata.svc/$metadata/?format=json
4

6 回答 6

23

$metadata文档采用 CSDL 格式,目前只有 XML 表示。(附带说明,如果您确实想为不同类型的 OData 有效负载请求 json 格式,请确保format查询令牌$前面有一个:$format=json。)

所以,不,这是不可能的。但是,您可以获取 JSON 格式的服务文档,它是 $metadata 文档的子集:

http://odata.informea.org/services/odata.svc?$format=json

这不会有类型信息,但会列出服务的可用入口点(即实体集)。

于 2013-09-09T01:25:26.373 回答
1

我同意前面的回答。规范不支持此功能,但一些 OData 框架/库即将实现此功能。

我想到了奥林戈。如果您还实现服务器端,这可能对您有所帮助。有关更多详细信息,请参阅 Olingo JIRA 中的此问题:

希望它可以帮助你,蒂埃里

于 2015-02-24T10:11:25.447 回答
1

作为替代?$format=json,您也可以只设置以下两个标题:

  • Accept: application/json
  • Content-Type: application/json; charset=utf-8

我不确定哪个是所需的最低 Odata 版本,但这在使用 Odata v4 的 Microsoft Dynamics NAV 2016 上非常适合我。

于 2016-02-21T22:23:17.807 回答
1

您可以使用 jQuery 从 OData 服务 $metadata 获取相关信息。

举个例子:
您编写了一个单元测试来检查 OData 实体属性名称是否与您的应用程序实体匹配。然后您必须检索 OData 实体的属性。

$.ajax({
            type: "GET",
            url: "/destinations/odata-service/$metadata",
            beforeSend: function() {
                console.log("before send check");
            },
            dataType: "xml",
            contentType: "application/atom+xml",
            context: document.body,
            success: function(xml) {
                console.log("Success ResourceTypes");   
                var ODataTypeINeed = $(xml).find('EntityType').filter(function(){ 
                                         return $(this).attr('Name') == 'ODataTypeINeed'
                                    });                 
                $(ODataTypeINeed).find('Property').each(function() {
                    console.log($(this).attr('Name')); //List of OData Entity properties
                });
            },
            error: function(err) {
                console.log(err);
            }
 });
于 2017-11-12T16:24:27.797 回答
1

我编写了一个简单的提供程序来从元数据中解析出一些需要的信息,请随意扩展它。首先,您需要一些简单的模型来表达数据,我们要从那里转换丑陋的 XML 名称

export class ODataEntityType
{
    name: string;
    properties: ODataProperty[];
}

export class ODataProperty
{
    name: string;
    type: ODataTypes;
    isNullable: boolean;
}

//Hack Until Ionic supports TS 2.4
export class ODataTypeMap
{
    "Edm.Int32" = ODataTypes.Int;
    "Edm.Int64" = ODataTypes.Long;
    "Edm.Decimal" = ODataTypes.Decimal;
    "Edm.Double" = ODataTypes.Double;
    "Edm.Guid" = ODataTypes.Guid;
    "Edm.String" = ODataTypes.String;
    "Edm.Boolean" = ODataTypes.Bool;
    "Edm.DateTime" = ODataTypes.DateTime;
    "Edm.DateTimeOffset" = ODataTypes.DateTimeOffset;
}

export enum ODataTypes
{
    Int,
    Long,
    Decimal,
    Double,
    Guid,
    String,
    Bool,
    DateTime,
    DateTimeOffset
}

这是提供者:

import { Injectable } from "@angular/core";
import { Http } from "@angular/http";
import * as X2JS from 'x2js';
import * as _ from 'underscore';
import { ODataEntityType, ODataProperty, ODataTypes, ODataTypeMap } from "../models/ODataEntityType";

@Injectable()
export class ODataMetadataToJsonProvider  {

    x2js = new X2JS();

    public entityTypeMap: Dictionary = new Dictionary();

    public entityTypes : ODataEntityType[];

    constructor(public http: Http) {
    }

    parseODataMetadata(metadataUrl: string) {
        this.http.get(metadataUrl).subscribe(data => {
            let metadata: any = this.x2js.xml2js(data.text());

            let rawEntityTypes = _.filter(metadata.Edmx.DataServices.Schema, x => x["EntityType"] != null);

            if(rawEntityTypes.length == 0)
            {
            return;
            }

            this.entityTypes =  _.map(rawEntityTypes[0]["EntityType"], t => { 
                let oDataEntityType = new ODataEntityType();
                oDataEntityType.name = t["_Name"];
                oDataEntityType.properties = _.map(t["Property"], p => {
                    let property = new ODataProperty();
                    property.name = p["_Name"];
                    let typeStr: string = p["_Type"];
                    property.type = ODataTypeMap[typeStr];
                    property.isNullable = !!p["_Nullable"];
                    return property;
                });

                return oDataEntityType;
            });
        });
    }
}
于 2018-04-07T05:48:59.060 回答
0

2021年,可能...

OData 4.01 规范包括对 JSON 的支持:https ://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-protocol.html#sec_MetadataDocumentRequest

Microsoft 的 OData 实施增加了对以下版本的支持:

  • ODataLib 7.7.3(2020 年 9 月 24 日发布)
  • WebAPI 7.5.4(2020 年 12 月 29 日发布)

此外,JSON 元数据是only supported at platform implementing .NETStardard 2.0.[原文如此]。

如果您调用的服务支持它,您可以在查询字符串中执行它...

  • $format=application/json
  • $format=json

...或使用 http 标头:

  • Accept=application/json

它不...要求服务提供商升级?

于 2021-02-18T00:44:19.167 回答