Java三種方式實現傳送xml引數的WebService介面呼叫
專案開發中與第三方系統資料對接遇到的問題,僅用作記錄。
1.使用cxf呼叫(聯調時沒有收到響應資訊)
JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance(); logger.info(JSON.toJSON(todoInfo)); Client client = clientFactory.createClient("http://******************?wsdl"); String[] result = (String[]) client.invoke("ummWaitMessageAdd", todoInfo.get("sysno"), todoInfo.get("iccode"), todoInfo.get("msgno"), todoInfo.get("pkno") , todoInfo.get("gno"), todoInfo.get("title"), todoInfo.get("type"), todoInfo.get("url"), todoInfo.get("info"), todoInfo.get("prior") , todoInfo.get("flow"), todoInfo.get("create"), todoInfo.get("create")); logger.info(result.toString()); String resultStr = result[0]; return resultStr;
相關jar包,也可點選下載
pom.xml
<cxf.version>2.7.12</cxf.version>
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-core</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-simple</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-databinding-aegis</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-local</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-jms</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-management</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-common-utilities</artifactId> <version>2.5.10</version> </dependency>
2.使用axis方式呼叫(這種方式可以收到服務端返回資訊,不過一直報錯)
org.apache.axis.client.Service service = new org.apache.axis.client.Service();
Call call = (Call) service.createCall();
String endpoint = "http://*****************************?wsdl";
String operationName = "ummWaitMessageAdd";
call.setTargetEndpointAddress(new URL(endpoint));
call.setOperationName(new QName(targetNameSpace, operationName));
call.addParameter("sysno", Constants.XSD_STRING, ParameterMode.IN);
call.addParameter("iccode", Constants.XSD_STRING, ParameterMode.IN);
call.addParameter("msgno", Constants.XSD_STRING, ParameterMode.IN);
call.addParameter("pkno", Constants.XSD_STRING, ParameterMode.IN);
call.addParameter("gno", Constants.XSD_STRING, ParameterMode.IN);
call.addParameter("title", Constants.XSD_STRING, ParameterMode.IN);
call.addParameter("type", Constants.XSD_STRING, ParameterMode.IN);
call.addParameter("url", Constants.XSD_STRING, ParameterMode.IN);
call.addParameter("info", Constants.XSD_STRING, ParameterMode.IN);
call.addParameter("prior", Constants.XSD_STRING, ParameterMode.IN);
call.addParameter("flow", Constants.XSD_STRING, ParameterMode.IN);
call.addParameter("create", Constants.XSD_STRING, ParameterMode.IN);
call.addParameter("update", Constants.XSD_STRING, ParameterMode.IN);
//設定返回的型別
call.setReturnType(Constants.XSD_STRING);
// 這裡的obj{}是放入幾個入參,完全由service提供的介面方法的入參決定,且順序和你存放的順序一致!一般入參為String型別的xml報文,回參也是xml報文。
Object[] obj = new Object[] { todoInfo.get("sysno")+"", todoInfo.get("iccode"), todoInfo.get("msgno"), todoInfo.get("pkno")
, todoInfo.get("gno"), todoInfo.get("title"), todoInfo.get("type"), todoInfo.get("url"), todoInfo.get("info"), todoInfo.get("prior")
, todoInfo.get("flow"), todoInfo.get("create")+"", todoInfo.get("create")+"" };
String result = (String) call.invoke(obj);
return result;
相關jar包,也可點選下載
pom.xml
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>axis</groupId>
<artifactId>axis-wsdl4j</artifactId>
<version>1.5.1</version>
</dependency>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.2</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
3.通過http post方式(最後就是通過這種方式實現介面...)
先將請求引數封裝在xml中,在傳送http請求
/**
* 生成請求xml資料
* @param methodName 方法名 本例為"ummWaitMessageAdd"
* @param todoInfo 資料 (key為wsdl檔案中引數的name值注意大小寫和順序都要保持一致,value為實際值)
* @return
*/
private String makeXml(String methodName ,Map<String, Object> todoInfo) {
logger.info("=======生成xml======");
StringBuffer sb = new StringBuffer();
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
" <soap:Body>\n" +
" <" + methodName + " xmlns=\"WSCenter\">\n" );
for (String key : todoInfo.keySet()) {
if (DataUtil.checkMapcontainsKey(todoInfo, key)) {
sb.append("<" + key + ">");
sb.append(todoInfo.get(key).toString().replaceAll("&", "&"));//特殊字元需要轉換
sb.append("</" + key + ">");
} else {
sb.append("<" + key + "/>");
}
sb.append("\r\n");
}
sb.append( " </" + methodName + ">\n" +
" </soap:Body>\n" +
"</soap:Envelope>");
logger.info("=======生成xml結束======");
return sb.toString();
}
傳送請求程式碼
URL url = new URL("http://*****************************?wsdl");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestMethod("POST");
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.write(params.getBytes("utf-8"));//params就是上面生成的xml內容
dos.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
String line = null;
StringBuffer strBuf = new StringBuffer();
while ((line = reader.readLine()) != null) {
strBuf.append(line);
}
dos.close();
reader.close();
String rs = strBuf.toString();
webservice服務端wsdl檔案(服務端是用.net實現)