一般有三种方式
调用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>
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("服务器异常,请联系管理员");
}
/**
* 功能描述: 发起通用请求
*
* @param paramMap 参数 map
* @param thisPath 请求路径
* @param actionNo actionNo
* @return : org.springblade.core.tool.api.R
* @author : yzd e-mail: [email protected]
* @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 = "\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" " + actionNo + "\n" +
" \n" +
" + xmlStr +
"]]> \n" +
" \n" +
" \n" +
"";
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);