平台介绍-开放API接口-IO说明
IO遵循平台内部API规范,接口入参出参和内部用是完全一样的。
很多平台的SDK需要客户端自己封装和解析json,本平台不是这个思路,而是使用DTO来封装,因为很多接口本来就是对内对外一样的,例如根据id返回人员的接口:
服务端:
@PostMapping("/person/findPersonInfo") @Operation(summary = "获取人员信息") public ResponseResult<PersonInfoDto> findPersonInfo(@RequestBody RequestInfo requestInfo)
SDK对应函数:
package org.qlm.access.sdk.service; public class CoreHRService {public static ResponseResult<PersonInfoDto> findPersonInfo(RequestInfo requestInfo) throws Exception{...} }
这也就是为什么平台强调要把dto层单独拿出来做单独工程,封装成独立jar的原因。这样dto可以共享到很多对方。
SDK和服务端通讯引用的是
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.6</version> </dependency>
通讯返回的包结构如下:
public class SDKHttpResponse {public static int SUCCESS = 200;private int status;private String body; }
最核心的技术是从SDKHttpResponse解析出真正需要的DTO结构
SDKHttpResponse中有静态方法:
public <T> ResponseResult<T> toResponseResult(RequestInfo req,Class<T> clazz){if (SUCCESS == this.getStatus()) {// 通讯成功Gson gson = new Gson();ResponseResult<T>result=new ResponseResult<T>();JsonObject resultObj = gson.fromJson(this.getBody(), JsonObject.class);result.setRetCode(resultObj.get("retCode").getAsInt());result.setMsg(resultObj.get("msg").getAsString());result.setFlag(resultObj.get("flag").getAsString());result.setVersion(resultObj.get("version").getAsString());result.setTimestamp(resultObj.get("timestamp").getAsString());result.setConsumer(resultObj.get("consumer").getAsString());result.setServer(resultObj.get("server").getAsString());result.setPath(resultObj.get("path").getAsString());result.setData(gson.fromJson(resultObj.get("data"),clazz));return result;} else { // 通讯失败ResponseResult result=new ResponseResult();result.setRetCode(IOResultEnum.COMM_FAIL.getKey());result.setMsg("HTML Code:"+this.getStatus());result.setSerialNo(req.getSerialNo());return result;} }