关于wsdl接口对于我来说是比较头疼的 基本没搞过。一脸懵 就在网上搜 看着写的都很好到我这就不好使了,非常蓝瘦、谨以此随笔纪念我这半个月踩过的坑、、、
背景:短短两周除了普通开发外我就接到了两个webservice接口,很少搞这样的接口 就很懵;
开发工具:eclipse、idea
方法一:
第一种就是个人觉得很容易懂得方法,作为wsdl接口,我们可以用开发工具将wsdl接口转为本地文件,这样就可以看他们的调用方法以及参数了。
·以eclipse为例,在你的项目里右键选择other
·然后搜索web,选择web service client
·之后将你的wsdl路径输入,点击finish 就可以看到了
·这个时候写个main方法像调本地方法一样去调用就好了
·idea的这里将生成路径说一下:
·一般wsdl调用比较正规的话 具体调用就会写在XXXservicePort.java里 然后写个调用方法去调用就好了
·另外附上我的测试:
ps:因为我这边开发有代理需要加上代理就加上代理访问的(这也是个坑啊!!!)
//给java进程强制加代理 System.getProperties().put("proxySet", "true"); System.getProperties().put("proxyHost", "172.30.XX.XXX"); System.getProperties().put("proxyPort", "9999"); System.out.println("Hello World!"); TaskService service = new TaskService(); TaskServicePort port = service.getTaskServicePort(); B2BParameter para1 = new B2BParameter(); B2BParameter para2 = new B2BParameter(); B2BParameter para3 = new B2BParameter(); B2BParameter para4 = new B2BParameter(); B2BParameter para5 = new B2BParameter(); B2BParameter para6 = new B2BParameter(); para1.setName("loginId"); para1.setValue("AWP_B2B_CN"); para2.setName("password"); para2.setValue("audatex"); para3.setName("maxCount"); para3.setValue("100"); para4.setName("startAtIndex"); para4.setValue("1"); para5.setName("fieldsToReturn"); para5.setValue("ResponsibleUserLoginId,CreatorLoginId,CreationDate,CaseId,TaskId,ClaimNumber,ManufacturerName,ModelName,PlateNumber,VIN,BusinessStatusKind"); para6.setName("returnPayloadAsXML"); para6.setXsltParameter(true); B2BRequest request = new B2BRequest(); request.getParameter().add(para1); request.getParameter().add(para2); request.getParameter().add(para3); request.getParameter().add(para4); request.getParameter().add(para5); request.getParameter().add(para6); request.setPayload("CreationDate >\"2019-07-01T11:00:00\" and CreationDate < \"2019-08-08T12:00:00\" "); B2BResponse response = port.findTasks(request); System.out.println(response.getHostName()); System.out.println(response.getLoginId()); System.out.println(response.getPayload()); System.out.println(response.getReturnCode());
方法二:
另外就是用httpClient的方式去调用。下面我将附上我的代码,希望能帮到(pps:有加代理)
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;
// 这里引得依赖 包的话需要自己找了 下面地址可以找到
//https://mvnrepository.com/
public static InputStream postXmlRequestInputStream(String requestUrl, String xmlData) throws IOException{ PostMethod postMethod = new PostMethod(requestUrl); byte[] b = xmlData.getBytes("utf-8"); InputStream is = new ByteArrayInputStream(b, 0, b.length); RequestEntity re = new InputStreamRequestEntity(is, b.length, "text/xml;charset=utf-8"); postMethod.setRequestEntity(re); HttpClient httpClient = new HttpClient(); httpClient.getParams().setAuthenticationPreemptive(true); httpClient.getHostConfiguration().setProxy(CommonPptsUtil.get("PROXY_HOST"), Integer.valueOf(CommonPptsUtil.get("PROXY_PORT"))); int statusCode = httpClient.executeMethod(postMethod); logger.debug("responseCode:"+statusCode); if (statusCode != 200) { return null; } return postMethod.getResponseBodyAsStream(); } public static void main(String[] args) { String reqJsonStr = "{\"workId\":\"20171018161622\",\"status\":\"201\",\"startTime\":\"2017-10-18 16:16:22\"}"; String xmlData = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.interfacemodule.cic.com/\"><soapenv:Header/><soapenv:Body><ser:statusWriteBack><jsonString>" + "{\"workId\":\"314\",\"orderId\":\"5207675\",\"longitude\":\"104.068310\",\"latitude\":\"30.539503\",\"sendTime\":\"2019-08-13 08:38:45\",\"servicePerName\":\"于xx\",\"servicePerPhone\":\"184xxxx7680\"}" + "</jsonString></ser:statusWriteBack></soapenv:Body></soapenv:Envelope>"; String url = "http://xx.xxx.246.88:7103/avs/services/CCService?wsdl"; SAXReader reader = new SAXReader(); String result = ""; try { InputStream in = postXmlRequestInputStream(url,xmlData); if(in!=null){ Document doc = reader.read(in); result = doc.getRootElement().element("Body").element("statusWriteBackResponse").element("return").getText(); logger.debug("result:"+result); } } catch (Exception e) { logger.error("error:",e); e.printStackTrace(); } }
CommonPptsUtil://就是获取配置文件里的代理信息
import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy; import org.apache.log4j.Logger; /** * 通用属性文件工具类 * * @author y.c * */ public class CommonPptsUtil { private static final Logger logger = Logger.getLogger(CommonPptsUtil.class); private static final String CONFIG_FILE = "common.properties"; private static PropertiesConfiguration ppts; static { try { ppts = new PropertiesConfiguration(CONFIG_FILE); ppts.setReloadingStrategy(new FileChangedReloadingStrategy()); } catch (ConfigurationException e) { logger.error("文件【common.properties】加载失败!"); ppts = null; } } /** * 获取属性值 * * @param key * @return 属性值 */ public static String get(String key) { if (ppts == null) { return null; } return ppts.getString(key); } }