接口平台支持的协议
目前接口平台只是支持了HTTP、HTTPS(1.1.0.RELEASE以后)、SOAP。
需要支持其他协议怎么办
目前虽然接口平台只是支持REST、SOAP风格的协议,但是我们提供了扩展的能力。用户只需要在接口平台的LOV服务类型LOV中新加一个LOV类型并实现org.hzero.boot.interfaces.sdk.invoke.category.InterfaceInvoker接口并注入到容器中就可以实现其他协议的透传。(需要注意InterfaceInvoker.targetType的返回值为LOV中新加的类型,invoke(…)方法实现具体调用逻辑)
协议转换
目前接口平台仅仅支持SOAP/REST两种接口风格的协议互转。用户只需要编辑接口,选择发布类型即可。
调用
REST风格调用
通过SDK调用接口
添加依赖
<dependency>
<groupId>org.hzero.boot</groupId>
<artifactId>hzero-boot-interface</artifactId>
<version>${hzero.boot.version}</version>
</dependency>
@Autowired
private InterfaceInvokeSdk interfaceInvokeSdk;
public void testMethod(){
RequestPayloadDTO requestPayloadDTO = new RequestPayloadDTO();
// 目标接口头部参数
Map<String,String> headerParamMap = new HashMap<>();
requestPayloadDTO.setHeaderParamMap(headerParamMap);
// 目标接口路径参数
Map<String,String> pathParamMap = new HashMap<>();
requestPayloadDTO.setBodyParamMap(pathParamMap);
// 目标接口URL参数
Map<String,String> urlParamMap = new HashMap<>();
requestPayloadDTO.setRequestParamMap(urlParamMap);
//注意此参数十分重要。
// 在REST风格透传的时候它用于指定请求目标接口的Content-Type以及用此Content-type对body进行编码,若补指定编码默认使用UTF-8,若不传MediaType默认使用application/json。
// 在SOAP风格透传的时候会以此媒体类型对SOAP报文进行编码,若不指定编码规则默认使用UTF-8编码,若不传Content-Type默认使用application/soap+xml。
requestPayloadDTO.setMediaType("");
/**
* 针对form_data 以及 url_encode(1.1版本以后)我们将参数调整到了bodyParamMap 以及multiPartFileMap
*/
requestPayloadDTO.setPayload("{}");
interfaceInvokeSdk.invoke("namespace","serverCode","interfaceCode",requestPayloadDTO);
}
通过发布地址调用
// 获取token
HttpPost getTokenReq = new HttpPost();
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
Map<String,String> userInfo = new HashMap<>();
userInfo.put("userName","xxx");
userInfo.put("password","xxx");
userInfo.put("grant_type","xxx");
userInfo.put("client_id","xxx");
userInfo.put("client_secret","xxx");
userInfo.forEach((k, v) -> multipartEntityBuilder.addTextBody(k, v));
getTokenReq.setEntity(multipartEntityBuilder.build());
getTokenReq.setURI(new URI("tokenUrl"));
HttpResponse httpResponse = HttpClients.createDefault().execute(getTokenReq);
Map<String,String>resultMap = new ObjectMapper().readValue(EntityUtils.toString(httpResponse.getEntity()),Map.class);
String token = resultMap.get("access_token");
// 开始调用发布地址
HttpPost httpPost = new HttpPost();
httpPost.setHeader("Authorization","Bearer "+token);
// 按照sdk调用给好值
RequestPayloadDTO requestPayloadDTO = new RequestPayloadDTO();
StringEntity stringEntity = new StringEntity(new ObjectMapper().writeValueAsString(requestPayloadDTO));
httpPost.setURI(new URI("http://hzeronb.saas.hand-china.com/hitf/v1/rest/invoke?namespace=ZC-199510&serverCode=TEST-156885911&interfaceCode=TEST-156885911"));
HttpClients.createDefault().execute(httpPost);
SOAP风格调用
通过SDK调用
@Autowired
private InterfaceInvokeSdk interfaceInvokeSdk;
public void testMethod(){
RequestPayloadDTO requestPayloadDTO = new RequestPayloadDTO();
// 目标接口头部参数,WSDL 是没有URL以及PATH参数这个概念的所以无需指定其他参数map
Map<String,String> headerParamMap = new HashMap<>();
requestPayloadDTO.setHeaderParamMap(headerParamMap);
// 注意body参数只是<xmlnsPrefix:opreationName>标签里面的内容,相当于接口的请求参数,xml所有参数都是以body形式传递
requestPayloadDTO.setPayload("<paramName>paramValue</paramName>");
interfaceInvokeSdk.invoke("namespace","serverCode","interfaceCode",requestPayloadDTO);
}
通过发布地址调用
// 获取token
HttpPost getTokenReq = new HttpPost();
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
Map<String,String> userInfo = new HashMap<>();
userInfo.put("userName","xxx");
userInfo.put("password","xxx");
userInfo.put("grant_type","xxx");
userInfo.put("client_id","xxx");
userInfo.put("client_secret","xxx");
userInfo.forEach((k, v) -> multipartEntityBuilder.addTextBody(k, v));
getTokenReq.setEntity(multipartEntityBuilder.build());
getTokenReq.setURI(new URI("tokenUrl"));
HttpResponse httpResponse = HttpClients.createDefault().execute(getTokenReq);
Map<String,String>resultMap = new ObjectMapper().readValue(EntityUtils.toString(httpResponse.getEntity()),Map.class);
String token = resultMap.get("access_token");
// 开始调用发布地址
HttpPost httpPost = new HttpPost();
httpPost.setHeader("Authorization","Bearer "+token);
// 按照sdk调用给好值
String xml="<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:m="http://interface.hzero.org/v1/">
<soap:Body>
<m:invoke >
<interfaceCode>CLIENTS</interfaceCode>
<serverCode>COCO</serverCode>
<namespace>HZERO</namespace>
<payload>
<!-- 目标接口路径参数,若目标接口是SOAP无需设置此参数 -->
<pathVariableMap>
<!-- key代表参数名称,标签值代表参数值 -->
<dataEntryList key="organization_id">0</dataEntryList>
</pathVariableMap>
<!-- 目标接口url问号后面的参数,若目标接口是SOAP无需设置此参数 -->
<requestParamMap></requestParamMap>
<!-- 目标接口头部参数:若目标地址是SOAP,我们通常会再次设置Content-Type 以及SOAPAction字段 -->
<headerParamMap></headerParamMap>
<mediaType>Content-Type: application/JSON</mediaType>
<!-- 这里需要注意,如果目标地址是SOAP,我们在此填写目标地址对应的接口<opreationName>标签里面的xml片段,并且我们需要用<![CDATA[xml片段]]转义,若是json,直接写json字符串就好-->
<payload>xxx</payload>
</payload>
</m:invoke>
</soap:Body>
</soap:Envelope>";
StringEntity stringEntity = new StringEntity(xml);
httpPost.setURI(new URI("http://hzeronb.saas.hand-china.com/hitf/v1/soap/invoke"));
HttpClients.createDefault().execute(httpPost);