方式1.代理類工廠的方式,需要拿到對方的接口
try {
// 接口地址
// 代理工廠
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
// 設置代理地址 wsdlAddress: WSDL地址(http://localhost:8082/ws/services/bank?wsdl)
jaxWsProxyFactoryBean.setAddress(wsdlAddress);
// 設置接口類型
jaxWsProxyFactoryBean.setServiceClass(ICommonService.class);
// 創建一個代理接口實現
ICommonService cs = (ICommonService) jaxWsProxyFactoryBean.create();
// 數據准備
String userName = "Leftso";
// 調用代理接口的方法調用並返回結果
String result = cs.sayHello(userName);
System.out.println("返回結果:" + result);
} catch (Exception e) {
e.printStackTrace();
}
方式2. 動態調用方式
//WSDL路徑
String wsUrl = "http://localhost:8082/ws/services/bank?wsdl" ; //方法名
String method = "getCaseProve"; JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance(); Client client = factory.createClient(wsUrl); Endpoint endpoint = client.getEndpoint(); QName opName = new QName(endpoint.getService().getName().getNamespaceURI(), method); BindingInfo bindingInfo = endpoint.getEndpointInfo().getBinding(); System.out.println(client); if (bindingInfo.getOperation(opName) == null) { for (BindingOperationInfo operationInfo : bindingInfo.getOperations()) { if (method.equals(operationInfo.getName().getLocalPart())) { opName = operationInfo.getName(); break; } } }
//請求報文 String xmlInput= "<Request RequestType=\"RegInfo\"><MachineInfo MachineAccount=\"1\" MachinePassword=\"1\"></MachineInfo><FiterInfo UnitNo=\"" + unit + "\" HouseID=\"" + houseId + "\" RecHouseNum=\"" + recHouseNum + "\"></FiterInfo></Request>"; Object[] res = null; try { res = client.invoke(opName, xmlInput); String xml = (String) res[0]; System.err.println("@@@@@@@@@@@@@@@@@"+xml); } catch (Exception e) { e.printStackTrace(); }
方式2所需jar 百度雲盤
鏈接:https://pan.baidu.com/s/15UjnqWU3J6Sx6CibizyqFw
提取碼:yjfl
方式3. HttpURLConnection調用方式
第一步(讀取配置文件): public class PropertiesUtil { private static final Logger logger = LoggerFactory .getLogger(PropertiesUtil.class); private static Properties properties = null; private static String fileName = "/app.properties"; static { properties = new Properties(); try { properties.load(PropertiesUtil.class.getResourceAsStream(fileName)); } catch (IOException e) { logger.error("加載配置文件失敗", e); } } public static Properties getProperties() { return properties; } public static String getProp(String propName) { return properties.getProperty(propName); } }
public static Map<String, String> resultInterface(String id,String type, String jsonStr) { StringBuilder res = new StringBuilder(); // 1:創建服務地址 URL url; String resultUserName = PropertiesUtil.getProp("resultUserName"); String resultPassWord = PropertiesUtil.getProp("resultPassWord"); Map<String, String> map = new HashMap<String, String>(); try { url = new URL(PropertiesUtil.getProp("resultUrl")); // 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); //"{\"ANNOUNCEMENT_CODE\": \"CSGGBH001\",\"ANNOUNCEMENT_CONNECT\": \"asdfasdfasdfasdfasdfasdfasdfsa\",\"ANNOUNCEMENT_DEADLINE\": \"20181031\",\"ANNOUNCEMENT_GUID\": \"71F15848-C296-4A60-883C-223C55E9E8D8\",\"ANNOUNCEMENT_START_TIME\": \"20181101\",\"ANNOUNCEMENT_TITLE\": \"公告標題\",\"ANNOUNCEMENT_TYPE\": \"1\",\"ANNOUNCEMENT_UNIT\": \"單位\",\"ATTACHMENT_SET_CODE\": \"123\",\"CANCEL_REASON\": \"撤銷理由\",\"CHANGE_TIME\": \"20181101\",\"CONTACT_NUMBER\": \"13333333333\",\"CONTACT_PERSON\": \"聯系人\",\"CREATE_TIME\": \"20181101\",\"DATA_TIMESTAMP\": \"20181101154911\",\"EMAIL\": \"123@qq.com\",\"END_DATE\": \"20181101\",\"FIELD_NUM\": \"123\",\"LAND_DISTRICT\": \"440601\",\"LIAISON_UNIT\": \"單位\",\"LISTING_DEADLINE\": \"\",\"LISTING_START_TIME\": \"\",\"LISTING_TYPE\": \"0\",\"PLATFORM_CODE\": \"91441900708017879M\",\"PUBLISHING_TIME\": \"20181101\",\"PUB_SERVICE_PLAT_CODE\": \"123707003283632515\",\"RETREAT_TIME\": \"20181101\",\"SUPPLY_TYPE\": \"1\",\"UNIFIED_DEAL_CODE\": \"B01-123707003283632515-20181025-000015-V\",\"UNIT_ADDRESS\": \"單位地址\",\"URL\": \"http://www.baidu.com\",\"ZIP_CODE\": \"215600\"}"; //TDJYGG // 4:組織SOAP協議數據,發送給服務端 String soapXML = "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">" + " <soap12:Header> " + " <root xmlns=\"http://tempuri.org/\"> " + " <UserName>"+resultUserName+"</UserName> " + " <PassWord>"+resultPassWord+"</PassWord> " + " </root> " + " </soap12:Header> " + " <soap12:Body> " + " <Push xmlns=\"http://tempuri.org/\"> " + " <Type>"+type+"</Type> " + " <JsonStr>"+jsonStr+"</JsonStr>" + " </Push> " + " </soap12:Body> " + " </soap12:Envelope>"; OutputStream os = connection.getOutputStream(); os.write(soapXML.getBytes("utf-8")); // 5:接收服務端的響應 int responseCode = connection.getResponseCode(); if (200 == responseCode) {// 表示服務端響應成功 InputStream is = connection.getInputStream(); InputStreamReader isr = new InputStreamReader(is,"utf-8"); BufferedReader br = new BufferedReader(isr); String temp = null; while (null != (temp = br.readLine())) { res.append(temp); } is.close(); isr.close(); br.close(); String xml = res.toString(); Document doc = DocumentHelper.parseText(xml); // 將字符串轉為XML Element rootElt = doc.getRootElement(); // 獲取根節點 //System.out.println("根節點:" + rootElt.getStringValue()); Element nameElem = rootElt.element("Body").element("PushResponse").element("PushResult"); String code = nameElem.element("Code").getTextTrim(); String description = nameElem.element("Description").getTextTrim(); map.put("code", code); map.put("description", description); } else { //res.append("調接口異常!"); map.put("code", "500"); map.put("description", "數據推送失敗,服務端響應失敗!"); logger.error("數據推送失敗,服務端響應失敗!"); } os.close(); } catch (Exception e) { //res.append("調接口異常!"); map.put("code", "500"); map.put("description", "數據推送異常,推送信息為trans_notice表或trans_target表的id>>>>:" + id); logger.error("數據推送異常,推送信息為trans_notice表或trans_target表的id>>>>:" + id + "", e); } return map; }