Java 呼び出し Web サービス + XML インターフェイスのエクスペリエンスの概要
記事ディレクトリ
呼び出しメソッド
三种方式
通常、 webService インターフェイスへの呼び出しがあり、
-
HttpURLConnection によって呼び出される
-
apache-cxf を使用して Java クラス呼び出しを生成する
-
AXIS を使用して WebService を呼び出す
ここで使用しているのは、http 経由で Web サービスを呼び出すことであり、soapUI を使用して呼び出しをシミュレートできます。
hutool Maven 依存関係はここで紹介されています
<!-- hutool -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.10</version>
</dependency>
一般的な Web サービス呼び出し
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("服务器异常,请联系管理员");
}
複雑な Web サービス呼び出し
一般的なリクエストメソッドをカプセル化する
/**
* 功能描述: 发起通用请求
*
* @param paramMap 参数 map
* @param thisPath 请求路径
* @param actionNo actionNo
* @return : org.springblade.core.tool.api.R<cn.hutool.json.JSONObject>
* @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 = "<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);