15人参与 • 2025-07-21 • websocket
odataimpl.java
是 apache olingo odata 服务器核心组件中的关键实现类,负责创建和管理 odata 服务所需的各种组件。该类是抽象类 odata
的具体实现,采用工厂模式为 odata 服务提供各种序列化器、反序列化器、处理器等核心组件。
odataimpl 负责创建不同类型的序列化器:
odataserializer
): 用于标准的 odata 响应序列化edmassistedserializer
): 在缺少完整 edm 信息时使用。详见《olingo分析和实践——edm 辅助序列化器详解》edmdeltaserializer
): 用于增量响应序列化。fixedformatserializer
): 用于二进制或特定格式序列化提供多种反序列化器创建方式:
创建不同类型的 odata 请求处理器:
odatahttphandler
): 处理 http 上下文中的 odata 请求odatahandler
): 提供更底层的请求处理能力这是用户选择的重点方法,用于创建 edm 辅助序列化器:
@override public edmassistedserializer createedmassistedserializer(final contenttype contenttype) throws serializerexception { if (contenttype != null && contenttype.iscompatible(contenttype.application_json)) { return new edmassistedjsonserializer(contenttype); } throw new serializerexception("unsupported format: " + ((contenttype != null) ? contenttype.tocontenttypestring() : null), serializerexception.messagekeys.unsupported_format, ((contenttype != null) ? contenttype.tocontenttypestring() : null)); }
版本感知的重载方法:
@override public edmassistedserializer createedmassistedserializer(final contenttype contenttype, list<string> versions) throws serializerexception { iconstants constants = new constantsv00(); if(versions!=null && !versions.isempty() && getmaxversion(versions) > 4){ constants = new constantsv01() ; } if (contenttype != null && contenttype.iscompatible(contenttype.application_json)) { return new edmassistedjsonserializer(contenttype, constants); } throw new serializerexception("unsupported format: " + ((contenttype != null) ? contenttype.tocontenttypestring() : null), serializerexception.messagekeys.unsupported_format, ((contenttype != null) ? contenttype.tocontenttypestring() : null)); }
功能特点:
private float getmaxversion(list<string> versions) { float versionvalue [] = new float [versions.size()]; int i=0; float max=new float(0); for(string version:versions){ float ver = float.valueof(version); versionvalue[i++] = ver; max = max > ver ? max : ver ; } return max; }
算法特点:
if (serializer == null) { throw new serializerexception("unsupported format: " + ((contenttype != null) ? contenttype.tocontenttypestring() : null), serializerexception.messagekeys.unsupported_format, ((contenttype != null) ? contenttype.tocontenttypestring() : null)); }
odataimpl 作为工厂类,负责创建各种 odata 组件:
根据版本和内容类型选择不同的处理策略:
抽象父类定义创建组件的模板,子类实现具体逻辑:
// 创建 odata 实例 odata odata = odata.newinstance(); // 创建 json 序列化器 contenttype jsoncontenttype = contenttype.application_json; odataserializer serializer = odata.createserializer(jsoncontenttype); // 创建 xml 序列化器 contenttype xmlcontenttype = contenttype.application_xml; odataserializer xmlserializer = odata.createserializer(xmlcontenttype);
// 创建支持 v4.01 的序列化器 list<string> versions = arrays.aslist("4.01", "4.0"); odataserializer serializer = odata.createserializer( contenttype.application_json, versions);
// 创建 edm 辅助序列化器(用户选择的重点) edmassistedserializer edmserializer = odata.createedmassistedserializer( contenttype.application_json); // 创建版本感知的 edm 辅助序列化器 list<string> versions = arrays.aslist("4.01"); edmassistedserializer versionedserializer = odata.createedmassistedserializer( contenttype.application_json, versions);
// 创建 delta 序列化器 list<string> versions = arrays.aslist("4.01"); edmdeltaserializer deltaserializer = odata.createedmdeltaserializer( contenttype.application_json, versions);
// 创建基础反序列化器 odatadeserializer deserializer = odata.createdeserializer( contenttype.application_json); // 创建带元数据的反序列化器 servicemetadata metadata = odata.createservicemetadata( edmprovider, references); odatadeserializer metadatadeserializer = odata.createdeserializer( contenttype.application_json, metadata);
序列化器类型 | 用途 | 支持格式 |
---|---|---|
odataserializer | 标准 odata 响应序列化 | json, xml |
edmassistedserializer | 缺少 edm 信息时的序列化 | json |
edmdeltaserializer | 增量响应序列化 | json |
fixedformatserializer | 固定格式序列化 | binary, multipart |
内容类型 | 元数据级别 | 描述 |
---|---|---|
application_json | minimal | 最小元数据(默认) |
application_json | none | 无元数据 |
application_json | full | 完整元数据 |
application_xml | - | xml 格式 |
application_atom_xml | - | atom xml 格式 |
odataimpl.java
是 apache olingo odata 框架的核心工厂类,具有以下关键特点:
该类是整个 odata 服务架构的重要基础设施,为上层应用提供了统一、可靠的组件创建服务。通过理解这个类的设计和实现,可以更好地掌握 odata 服务的核心架构和运行机制。
到此这篇关于olingo分析和实践之odataimpl详细分析的文章就介绍到这了,更多相关olingo odataimpl内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论