Web服务 – 您的WebService版本控制最佳实践是什么?
我们有两个单独的产品,需要通过网络服务相互通信。
支持API的最佳实践是什么? 从2004年起,我有this article声称没有实际的标准,只有最佳做法。任何更好的解决方案?您如何解决WS版本控制? 问题描述 系统A 客户 class SystemAClient{ SystemBServiceStub systemB; public void consumeFromB(){ SystemBObject bObject = systemB.getSomethingFromB(new SomethingFromBRequest("someKey")); } } 服务 class SystemAService{ public SystemAObject getSomethingFromA(SomethingFromARequest req){ return new SystemAObjectFactory.getObject(req); } } 可转移对象 版本1 class SystemAObject{ Integer id; String name; ... // getters and setters etc; } 版本2 class SystemAObject{ Long id; String name; String description; ... // getters and setters etc; } 请求对象 版本1 class SomethingFromARequest { Integer requestedId; ... // getters and setters etc; } 版本2 class SomethingFromARequest { Long requestedId; ... // getters and setters etc; } 系统B 客户 class SystemBClient{ SystemAServiceStub systemA; public void consumeFromA(){ SystemAObject aObject = systemA.getSomethingFromA(new SomethingFromARequest(1)); aObject.getDescription() // fail point // do something with it... } } 服务 class SystemBService{ public SystemBObject getSomethingFromB(SomethingFromBRequest req){ return new SystemBObjectFactory.getObject(req); } } 可转移对象 版本1 class SystemBObject{ String key; Integer year; Integer month; Integer day; ... // getters and setters etc; } 版本2 class SystemBObject{ String key; BDate date; ... // getters and setters etc; } class BDate{ Integer year; Integer month; Integer day; ... // getters and setters etc; } 请求对象 版本1 class SomethingFromBRequest { String key; ... // getters and setters etc; } 版本2 class SomethingFromBRequest { String key; BDate afterDate; BDate beforeDate; ... // getters and setters etc; } 失败方案 如果系统A版本1的客户端调用版本2的系统B服务,它可能会失败: > SystemBObject(getYear(),getMonth(),getDay())的缺少方法 如果系统A版本2的客户端调用版本1的系统B服务,它可能会失败: >未知类型在SomethingFromBRequest上的BDate(客户端使用B版本1无法识别的较新的B请求对象) 如果版本1的系统B客户端调用版本2的系统A服务,它可能会失败: >在SystemAObject上键入missmatch或溢出(返回长但预期的整数) 如果版本2的系统B客户端调用版本1的系统A服务,则可能会失败: >在SystemARequest上键入missmatch或溢出(请求Long而不是整数) 可能的解决方案 >推进版本时使用数字:例如SystemAObject1,SystemBRequest2等,但是缺少一个用于匹配源/目标版本的API 解决方法
我更喜欢Salesforce.com的版本控制方法。每个版本的Web服务都获得一个不同的URL,格式为:
http://api.salesforce.com/{version}/{serviceName} 所以你会有一个Web服务URL: http://api.salesforce.com/14/Lead http://api.salesforce.com/15/Lead 等等… 通过这种方法,您将获得以下好处: >你总是知道你在说什么版本。>保持向后兼容性。>你不必担心依赖问题。每个版本都有完整的服务。您只需确保不要在呼叫之间混合版本(但这取决于服务的消费者,而不是您作为开发人员)。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |