java调用webservice+xml接口经验总结
调用方式
一般有三种方式
调用webService接口,
-
以HttpURLConnection的方式调用
-
使用apache-cxf生成java类调用
-
使用AXIS调用WebService
我这里使用的是,http方式调用webservice, 其中 可以使用 soapUI 进行模拟调用。
这边引入了hutool Maven 依赖
<!-- hutool -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.10</version>
</dependency>
一般webservice调用
HttpRequest post = HttpUtil.createPost("调用主机及路径");
// 参数
String param = URLDecoder.decode(data, StandardCharsets.UTF_8);
post.body(param);
// 发起请求
post.header("content-type", org.springframework.http.MediaType.APPLICATION_XML_VALUE.trim());
try {
HttpResponse execute = post.execute();
String trim = execute.body().trim();
JSONObject entries = JSONUtil.xmlToJson(trim);
// 可对改结果进行处理 或者 返回
} catch (Exception e) {
throw new ServiceException("服务器异常,请联系管理员");
}
复杂webservice调用
封装通用请求方法
/**
* 功能描述: 发起通用请求
*
* @param paramMap 参数 map
* @param thisPath 请求路径
* @param actionNo actionNo
* @return : org.springblade.core.tool.api.R<cn.hutool.json.JSONObject>
* @author : yzd e-mail: 121665820@qq.com
* @create : 2023/6/19 11:17
*/
@Override
public Map<String, Object> convertReqUtil(Map<String, Object> paramMap,
String thisPath,
String actionNo) {
if (Func.isEmpty(paramMap)) {
log.error("接口调用 path: {},param:{}", thisPath, paramMap);
throw new ServiceException("请求参数不能为空");
}
Document document = XmlUtil.mapToXml(paramMap, "Request");
String xmlStr = XmlUtil.toStr(document);
log.info("接口调用 path: {} ", thisPath);
log.info("接口调用 param:{}", xmlStr);
HttpRequest post = HttpUtil.createPost(thisPath);
post.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE);
post.header("SOAPAction", "HIPMessageService");
String stringBuffer = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:dhcc=\"http://www.dhcc.com.cn\">\n" +
" <soapenv:Header/>\n" +
" <soapenv:Body>\n" +
" <dhcc:HIPMessageService>\n" +
" <!--Optional:-->\n" +
" <dhcc:Action>" + actionNo + "</dhcc:Action>\n" +
" <!--Optional:-->\n" +
" <dhcc:Data><![CDATA[" + xmlStr +
"]]></dhcc:Data>\n" +
" </dhcc:HIPMessageService>\n" +
" </soapenv:Body>\n" +
"</soapenv:Envelope>";
post.body(stringBuffer);
try {
HttpResponse execute = post.execute();
String trim = execute.body().trim();
log.info("接口调用 resp: {}", trim);
return XmlUtil.xmlToMap(trim);
} catch (Exception e) {
log.error("接口调用 error:", e);
throw new ServiceException("服务器异常,请联系管理员");
}
}
发起请求并处理返回结果
Map<String, Object> paramMap = new HashMap<>(16);
// 身份证号
paramMap.put("cardNo", cardNo);
Map<String, Object> objectMap = this.convertReqUtil(paramMap,
UrlBuilder.of(ParamCache.getValueNoCache(ParamCache.YI_BIN_THIRD_HOSPITAL_HIS_PATH))
.addPath("/csp/hsb/DHC.Published.PUB0002.BS.PUB0002.CLS")
.build(),
"MES0189"
);
Map body = MapUtil.get(objectMap, "SOAP-ENV:Body", Map.class);
Map messageServiceResponse = MapUtil.get(body, "HIPMessageServiceResponse", Map.class);
String messageServiceResult = MapUtil.get(messageServiceResponse, "HIPMessageServiceResult", String.class);
return JSONUtil.parseObj(messageServiceResult).get("Response", PatientRepDTO.class);