文章目录
- 1. SoapUI 测试 WebService 接口
- 2. Java 访问 WebService 接口
1. SoapUI 测试 WebService 接口
通过SoapUI创建一个SOAP Project;
项目名称自定义,WSDL地址维护WebService接口地址。点击OK即可
项目创建完成后,展开WebService项,可以看到具体的接口,打开接口下的Request,右侧面板Form标签下可以清晰的看到请求入参,点击Submit请求按钮可以看到Overview标签下的响应结果。
XML标签下的请求报文和响应报文
<!-- 请求报文 -->
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservice.qyythpt.jzkj.com">
<soapenv:Header/>
<soapenv:Body>
<web:sendMessage>
<!--Optional:-->
<xmlStr>123456</xmlStr></web:sendMessage>
</soapenv:Body>
</soapenv:Envelope>
<!-- 响应报文 -->
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:sendMessageResponse xmlns:ns2="http://webservice.qyythpt.jzkj.com">
<String><![CDATA[<resident>1695087972825</resident>
<rescode>0</rescode>
<resmsg>数据处理错误!</resmsg>]]></String>
</ns2:sendMessageResponse>
</soap:Body>
</soap:Envelope>
2. Java 访问 WebService 接口
WebService 调用有多种方式,此处以 HttpURLConnection 调用为例。
接口请求封装,仅需要传入接口需要的入参和接口地址。
接口响应结果,解析成Map对象返回。如下所示
/**
* WebService推送报文封装请求推送
* WebService 接口地址为 http://{ip}:{port}/services/TraderService?wsdl
* @param webServicePushUrl 此处 webServicePushUrl 为 http://{ip}:{port}/services/TraderService
* @param xmlStr 接口入参
* @return
*/
private Map<String, Object> webServicePush(String webServicePushUrl, String xmlStr) {
Map<String, Object> resultMap = new HashMap<>(2);
resultMap.put("code", "0");
resultMap.put("msg", "推送失败!");
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
OutputStream os = null;
try {
//第一步:创建服务地址,不是WSDL地址
URL url = new URL(webServicePushUrl);
//2:打开到服务地址的一个连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//3:设置连接参数
//3.1设置发送方式:POST必须大写
connection.setRequestMethod("POST");
//3.2设置数据格式:Content-type
connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
//3.3设置输入输出,新创建的connection默认是没有读写权限的,
connection.setDoInput(true);
connection.setDoOutput(true);
//4:组织SOAP协议数据,发送给服务端;报文参考SoapUI测试工具的XML请求报文
String soapXML = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.qyythpt.jzkj.com\">" +
"<soapenv:Header/>" +
"<soapenv:Body>" +
"<web:sendMessage>" +
"<xmlStr><![CDATA[" +
xmlStr +
"]]></xmlStr></web:sendMessage>" +
"</soapenv:Body>" +
"</soapenv:Envelope>";
logger.warn("[WebService]:推送请求{}", soapXML);
os = connection.getOutputStream();
os.write(soapXML.getBytes());
//5:接收服务端的响应
int responseCode = connection.getResponseCode();
if(200 == responseCode){//表示服务端响应成功
is = connection.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String temp = null;
while(null != (temp = br.readLine())){
sb.append(temp);
}
logger.warn("[WebService]:推送结果{}", sb);
Map<String, Map> bodyMap = XmlUtils.xmlTOMap(sb.toString());
Map<String, Map> sendMessageResponseMap = bodyMap.get("Body");
Map<String, String> resultStringMap = sendMessageResponseMap.get("sendMessageResponse");
String stringXML = resultStringMap.get("String");
stringXML = "<root>" + stringXML + "</root>";
Map<String, String> stringMap = XmlUtils.xmlTOMap(stringXML);
// String -> <resident>1694505193442</resident><rescode>1</rescode><resmsg>ok</resmsg>
// String -> <resident>1678862927956</resident><rescode>0</rescode><resmsg>数据处理错误!</resmsg>
// 0失败,1成功
resultMap.put("code", stringMap.get("rescode"));
resultMap.put("msg", stringMap.get("resmsg"));
}
} catch (Exception e) {
logger.error("[WebService]推送异常", e);
} finally {
if (null != is) {
try {
is.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (null != isr) {
try {
isr.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (null != br) {
try {
br.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (null != os) {
try {
os.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
return resultMap;
}
更多请求方式可参考文档调用webservice服务方式总结
Powered By niaonao