简体   繁体   English

获取 JSON 格式的 OData $metadata

[英]Get OData $metadata in JSON format

is it possible to get metadata of an OData service in JSON format?是否可以以 JSON 格式获取 OData 服务的元数据?

When I try to use format=json , it doesn't work.当我尝试使用format=json ,它不起作用。 Here is what I tried:这是我尝试过的:

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

The $metadata document is in the CSDL format, which currently only has an XML representation. $metadata文档采用 CSDL 格式,目前只有 XML 表示。 (As a side note, if you do want to request the json format for a different kind of OData payload, make sure the format query token has a $ in front of it: $format=json .) (附带说明,如果您确实想为不同类型的 OData 负载请求 json 格式,请确保format查询令牌前面有一个$$format=json 。)

So, no it is not possible.所以,不,这是不可能的。 You can, however, get the service document in JSON, which is a subset of the $metadata document:但是,您可以获取 JSON 格式的服务文档,它是 $metadata 文档的一个子集:

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

This won't have type information, but it will list the available entry points of the service (ie, the entity sets).这不会有类型信息,但会列出服务的可用入口点(即实体集)。

I agreed with the previous answer.我同意之前的回答。 This isn't supported by the specification but some OData frameworks / libraries are about to implement this feature.规范不支持此功能,但一些 OData 框架/库即将实现此功能。

I think about Olingo.我想到了奥林戈。 This is could be helpful for you if you also implement the server side.如果您还实现了服务器端,这可能对您有所帮助。 See this issue in the Olingo JIRA for more details:有关更多详细信息,请参阅 Olingo JIRA 中的此问题:

Hope it helps you, Thierry希望对你有帮助,蒂埃里

As an alternative to ?$format=json , you could also just set the following two headers :作为?$format=json的替代方法,您还可以设置以下两个标头:

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

I'm not sure which is the minimum Odata version required, but this works perfectly for me on Microsoft Dynamics NAV 2016, which uses Odata v4.我不确定哪个是所需的最低 Odata 版本,但这在 Microsoft Dynamics NAV 2016 上非常适合我,它使用 Odata v4。

You can use jQuery to get the relevant information from an OData service $metadata.您可以使用 jQuery 从 OData 服务 $metadata 获取相关信息。

Take for example:举个例子:
You write a unit test to check the OData entities property names matches with your application entities.您编写一个单元测试来检查 OData 实体属性名称是否与您的应用程序实体匹配。 Then you have to retrieve the properties of the OData entity.然后您必须检索 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);
            }
 });

I wrote a simple provider to parse out some of the needed information from the metadata, Feel free to expand on it.我写了一个简单的提供程序来从元数据中解析出一些需要的信息,请随意扩展它。 First you'll need some simple models, to expresss the data, we'll want to convert from there ugly XML names首先你需要一些简单的模型来表达数据,我们需要从那里转换丑陋的 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
}

This is the provider:这是提供者:

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;
            });
        });
    }
}

In 2021, possibly... 2021年,可能...

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

Microsoft's OData implementation added support from the following versions: Microsoft 的 OData 实现增加了以下版本的支持:

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

Additionally, JSON Metadata is only supported at platform implementing .NETStardard 2.0.此外, only supported at platform implementing .NETStardard 2.0. JSON 元数据only supported at platform implementing .NETStardard 2.0. [ sic ]. [原文]。

If the service you're calling supports it, you can do it in the query string...如果您调用的服务支持它,您可以在查询字符串中进行...

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

...or using the http headers: ...或使用 http 标头:

  • Accept=application/json

It it doesn't... ask the provider of the service to upgrade?不是...要求服务提供商升级吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM