java调⽤wsdl的webservice接⼝两种调⽤⽅式
关于wsdl接⼝对于我来说是⽐较头疼的基本没搞过。⼀脸懵就在⽹上搜看着写的都很好到我这就不好使了,⾮常蓝瘦、谨以此随笔纪念我这半个⽉踩过的坑、、、
背景:短短两周除了普通开发外我就接到了两个webservice接⼝,很少搞这样的接⼝就很懵;
开发⼯具:eclipse、idea
⽅法⼀:
第⼀种就是个⼈觉得很容易懂得⽅法,作为wsdl接⼝,我们可以⽤开发⼯具将wsdl接⼝转为本地⽂件,这样就可以看他们的调⽤⽅法以及参数了。
·以eclipse为例,在你的项⽬⾥右键选择other
·然后搜索web,选择web service client
·
之后将你的wsdl路径输⼊,点击finish 就可以看到了
·这个时候写个main⽅法像调本地⽅法⼀样去调⽤就好了
·idea的这⾥将⽣成路径说⼀下:
·⼀般wsdl调⽤⽐较正规的话具体调⽤就会写在XXXservicePort.java⾥然后写个调⽤⽅法去调⽤就好了
·另外附上我的测试:
ps:因为我这边开发有代理需要加上代理就加上代理访问的(这也是个坑啊!!!)
//给java进程强制加代理
System.out.println("Hello World!");
TaskService service = new TaskService();
TaskServicePort port = TaskServicePort();
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.setPayload("CreationDate >\"2019-07-01T11:00:00\" and CreationDate < \"2019-08-08T12:00:00\" ");
B2BResponse response = port.findTasks(request);
System.out.HostName());
System.out.LoginId());
System.out.Payload());
System.out.ReturnCode());
⽅法⼆:
另外就是⽤httpClient的⽅式去调⽤。下⾯我将附上我的代码,希望能帮到(pps:有加代理)
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apachemons.httpclient.HttpClient;
import org.hods.InputStreamRequestEntity;
import org.hods.PostMethod;
import org.hods.RequestEntity;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;
// 这⾥引得依赖包的话需要⾃⼰了下⾯地址可以到
//mvnrepository/
public static InputStream postXmlRequestInputStream(String requestUrl, String xmlData) throws IOException{
PostMethod postMethod = new PostMethod(requestUrl);
byte[] b = Bytes("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();
int statusCode = uteMethod(postMethod);
logger.debug("responseCode:"+statusCode);
if (statusCode != 200) {
return null;
}
ResponseBodyAsStream();
}
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=\"/soap/envelope/\" xmlns:ser=\"service.interfacemodule.cic/\"><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 = "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 = ad(in);
result = RootElement().element("Body").element("statusWriteBackResponse").element("return").getText();
logger.debug("result:"+result);
}
} catch (Exception e) {
<("error:",e);
e.printStackTrace();
}
}
CommonPptsUtil://就是获取配置⽂件⾥的代理信息
import figuration.ConfigurationException;
import figuration.PropertiesConfiguration;
import loading.FileChangedReloadingStrategy;
import org.apache.log4j.Logger;
/**
* 通⽤属性⽂件⼯具类
*
* @author y.c
*
*/
public class CommonPptsUtil {
private static final Logger logger = Logger(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) {
<("⽂件【common.properties】加载失败!");
ppts = null;
}
}
/**
* 获取属性值
*
* @param key
* @return属性值
调用webservice服务*/
public static String get(String key) {
if (ppts == null) {
return null;
}
String(key);
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论