是否可以以JSON格式获取OData服务的元数据?
当我尝试使用format=json
时,它无法工作。以下是我的尝试:
http://odata.informea.org/services/odata.svc/$metadata/?format=json
$metadata
文档采用CSDL格式,目前仅具有XML表示形式。(顺带一提,如果您希望请求不同类型的OData有效负载的json格式,请确保查询令牌format
前面有一个$
: $format=json
。)
所以,不,这是不可能的。但是,您可以获取JSON格式的服务文档,它是$metadata文档的子集:
http://odata.informea.org/services/odata.svc?$format=json
这不会包含类型信息,但它将列出服务的可用入口点(即实体集)。
?$format=json
的替代方案,您也可以设置以下两个标头:
Accept: application/json
Content-Type: application/json; charset=utf-8
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;
});
});
}
}
$.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);
}
});
可能在2021年...
OData 4.01规范包括对JSON的支持:https://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part1-protocol.html#sec_MetadataDocumentRequest
微软的OData实现从以下版本开始添加支持:
此外,JSON Metadata 仅受.NETStardard 2.0实现的平台支持。
[原文如此]
如果您调用的服务支持它,可以在查询字符串中执行:
$format=application/json
$format=json
...或使用http标头:
Accept=application/json
如果不支持,请要求服务提供者升级?