我们有两个单独的产品,需要通过网络服务相互通信。
支持API的最佳实践是什么?
支持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())的缺少方法
>未知类型BDate
如果系统A版本2的客户端调用版本1的系统B服务,它可能会失败:
>未知类型在SomethingFromBRequest上的BDate(客户端使用B版本1无法识别的较新的B请求对象)
>如果System A客户端足够聪明地使用请求对象的版本1,则可能会在SystemBObject对象(getDate())上的缺少方法上失败
如果版本1的系统B客户端调用版本2的系统A服务,它可能会失败:
>在SystemAObject上键入missmatch或溢出(返回长但预期的整数)
如果版本2的系统B客户端调用版本1的系统A服务,则可能会失败:
>在SystemARequest上键入missmatch或溢出(请求Long而不是整数)
>如果请求通过某种方式转换问题(stub是Long,但服务返回一个整数在所有的WS实现中都不会非常兼容)
可能的解决方案
>推进版本时使用数字:例如SystemAObject1,SystemBRequest2等,但是缺少一个用于匹配源/目标版本的API
>在签名中,传递XML而不是对象(yuck,以XML格式传递转义的XML,双重序列化,反序列化/解析,解析)
>其他:例如文件/文字/ WS-I有补救吗?