最近一个项目需要调用一个webservice接口,遇到一个问题.项目中的jdom版本为0.9.而webservice client需要用到jdom1.0 如果将jdom版本升级则会造成现有系统异常.
因此需要在不改变现有项目jar的情况下解决这个问题.service端使用的jax-ws2.
wsdl如下:
因此需要在不改变现有项目jar的情况下解决这个问题.service端使用的jax-ws2.
wsdl如下:
Java代码<?xml version="1.0" encoding="UTF-8"?><!-- Published by JAX-WS RI at jax-ws.dev.java. RI's version is JAX-WS RI 2.1.1 in JDK 6. --><!-- Generated by JAX-WS RI at jax-ws.dev.java. RI's version is JAX-WS RI 2.1.1 in JDK 6. --><definitions xmlns:soap="/wsdl/soap/" xmlns:tns="www.hua-xia/ZySearch" xmlns:xsd="/2001/XMLSchema" xmlns="/wsdl/" targetNamespace="www.hua-xia/ZySearch" name="UserLinkWebServiceService">
<types></types>
<message name="getUserLink">
<types></types>
<message name="getUserLink">
<part name="linkNo" type="xsd:string"></part>
</message>
<message name="getUserLinkResponse">
<part name="returnVal" type="xsd:string"></part>
</message>
<portType name="UserLinkWebService">
<operation name="getUserLink" parameterOrder="linkNo">
<input message="tns:getUserLink"></input>
<output message="tns:getUserLinkResponse"></output>
</operation>
</portType>
<binding name="UserLinkWebServicePortBinding" type="tns:UserLinkWebService">
<soap:binding transport="/soap/http" ></soap:binding>
<operation name="getUserLink">
</message>
<message name="getUserLinkResponse">
<part name="returnVal" type="xsd:string"></part>
</message>
<portType name="UserLinkWebService">
<operation name="getUserLink" parameterOrder="linkNo">
<input message="tns:getUserLink"></input>
<output message="tns:getUserLinkResponse"></output>
</operation>
</portType>
<binding name="UserLinkWebServicePortBinding" type="tns:UserLinkWebService">
<soap:binding transport="/soap/http" ></soap:binding>
<operation name="getUserLink">
<soap:operation soapAction="getUserLink"></soap:operation>
<input>
<soap:body use="literal" namespace="www.hua-xia/ZySearch"></soap:body>
</input>
<output>
<soap:body use="literal" namespace="www.hua-xia/ZySearch"></soap:body>
</output>
</operation>
</binding>
<service name="UserLinkWebServiceService">
<port name="UserLinkWebServicePort" binding="tns:UserLinkWebServicePortBinding">
<soap:address location="192.168.1.1.154:9010/ZySearch"></soap:address>
</port>
<input>
<soap:body use="literal" namespace="www.hua-xia/ZySearch"></soap:body>
</input>
<output>
<soap:body use="literal" namespace="www.hua-xia/ZySearch"></soap:body>
</output>
</operation>
</binding>
<service name="UserLinkWebServiceService">
<port name="UserLinkWebServicePort" binding="tns:UserLinkWebServicePortBinding">
<soap:address location="192.168.1.1.154:9010/ZySearch"></soap:address>
</port>
</service>
</definitions>1.xfire调用
对方给我们的client是使用xfire的client调用,代码如下:
</definitions>1.xfire调用
对方给我们的client是使用xfire的client调用,代码如下:
Java代码
package com.zhongying.bussservmon;
import java.URL;
import java.util.Map;
dehaus.xfire.client.Client;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.l.DomDriver;
import java.util.Map;
dehaus.xfire.client.Client;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.l.DomDriver;
public class Test {
public static void main(String[] args) {
String url = "192.168.1.1:8000/RES_Searcher/service/IUserLinkWebService?wsdl";
Client client;
try {
client = new Client(new URL(url));
Object params[] = {"123456"};
String result = (String)client.invoke("getUserLink", params)[0];
XStream xStream = new XStream(new DomDriver());
Map map = (Map)xStream.fromXML(result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.axis调用
但是由于jar包的原因,我们不能使用上面的方法,想出的第一个解决方案是使用axis的客户端来调用接口,代码如下:
但是由于jar包的原因,我们不能使用上面的方法,想出的第一个解决方案是使用axis的客户端来调用接口,代码如下:
Java代码
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
l.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.l.DomDriver;
import com.thoughtworks.l.DomDriver;
public class TestReflection {
public static void main(String[] arg) {
String url = "192.168.1.1:9010/ZySearch";
try {
Service service = new Service();
Call call = (Call) ateCall();
// 设置调用服务地址
call.setTargetEndpointAddress(new java.URL(url));
//此处一定要配置wsdl的namespace参数www.hua-xia/ZySearch
call.setOperationName(new QName("www.hua-xia/ZySearch", "getUserLi
Call call = (Call) ateCall();
// 设置调用服务地址
call.setTargetEndpointAddress(new java.URL(url));
//此处一定要配置wsdl的namespace参数www.hua-xia/ZySearch
call.setOperationName(new QName("www.hua-xia/ZySearch", "getUserLi
nk"));
//此处需要配置传入参数类型与参数名称,如果未设置jax-ws则无法接受参数,会认为传入的参数为null
call.addParameter("linkNo",org.ding.XMLType.XSD_l.rpc.ParameterMode.IN);
//如果设置类传入参数类型,此处也需要设置返回参数类型
call.setReturnType(org.ding.XMLType.XSD_STRING);
call.setUseSOAPAction(true);
call.setSOAPActionURI(url);
call.addParameter("linkNo",org.ding.XMLType.XSD_l.rpc.ParameterMode.IN);
//如果设置类传入参数类型,此处也需要设置返回参数类型
call.setReturnType(org.ding.XMLType.XSD_STRING);
call.setUseSOAPAction(true);
call.setSOAPActionURI(url);
String result = (String) call.invoke(new Object[] { "AD0006526305" });
XStream xStream = new XStream(new DomDriver());
Map map = (Map) xStream.fromXML(result);
Map map = (Map) xStream.fromXML(result);
Iterator it = Set().iterator();
while (it.hasNext()) {
Map.Entry enty = (Entry) it.next();
System.out.Key() + ":" + Value());
}
} catch (Exception e) {
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
3.http模拟调用
由于开始对axis的了解有限,在写axis调用是总是有问题,于是便想了另外一个方法,这个方法有点剑走偏锋.如何生成webservice客户端但是适用性却很强,因为调用的是java自带的api不会产生兼容性问题.
大家知道webservice请求实际上也就是一个http请求,将数据通过xml文件进行交换.
既然知道了原理我们就可以模拟一个http请求来调用webservice.
大家知道webservice请求实际上也就是一个http请求,将数据通过xml文件进行交换.
既然知道了原理我们就可以模拟一个http请求来调用webservice.
Java代码
try{
String url2 = "192.168.1.1:9010/ZySearch?wsdl";
String url2 = "192.168.1.1:9010/ZySearch?wsdl";
URL getUrl = new URL(url2);
HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-type","text/xml");
connection.setRequestProperty("Accept","text/xml");
connection.setRequestProperty("User-Agent","JAX-WS RI 2.1.3-hudson-390-");
connection.setRequestProperty("Accept","text/xml");
connection.setRequestProperty("User-Agent","JAX-WS RI 2.1.3-hudson-390-");
String send="<soap:Envelope xmlns:soap=\"/soap/envelope/\" xmlns:xsd=\"/2001/XMLSchema\" xmlns:xsi=\"/2001/XMLSchema-instance\"><soap:Body><ns1:getUserLink xmlns:ns1=\"www.hua-xia/ZySearch\"><linkNo xmlns=\"\">"+par+"</linkNo></ns1:getUserLink></soap:Body></soap:Envelope>";
OutputStream().Bytes());
OutputStream().flush();
OutputStream().close();
t();
OutputStream().Bytes());
OutputStream().flush();
OutputStream().close();
t();
// 取得输入流,并使用Reader读取
BufferedReader reader = new BufferedReader(new InputStream(),"utf-8"));//设置编码,否则中文乱码
System.out.println("=============================");
System.out.println("Contents of get request");
System.out.println("=============================");
String lines="";
String reuslts = "";
while ((lines = adLine()) != null){
//lines = new Bhqytes(), "utf-8");
reuslts+=placeAll("<", "<").replaceAll(">", ">");
}
reader.close();
BufferedReader reader = new BufferedReader(new InputStream(),"utf-8"));//设置编码,否则中文乱码
System.out.println("=============================");
System.out.println("Contents of get request");
System.out.println("=============================");
String lines="";
String reuslts = "";
while ((lines = adLine()) != null){
//lines = new Bhqytes(), "utf-8");
reuslts+=placeAll("<", "<").replaceAll(">", ">");
}
reader.close();
System.out.println("type:"+ContentType());
// 断开连接
connection.disconnect();
System.out.println(reuslts);
System.out.println("=============================");
System.out.println("Contents of get request ends");
System.out.println("=============================");
XStream xStream = new XStream(new DomDriver());
reuslts = reuslts.substring(reuslts.indexOf("<returnVal>")+11);
reuslts = reuslts.substring(0,reuslts.indexOf("</returnVal>"));
System.out.println(reuslts);
Map result = (Map)xStream.fromXML(reuslts);
Iterator it = Set().iterator();
while(it.hasNext()){
Map.Entry enty = (Entry) it.next();
System.out.Key()+":"+Value());
}
System.out.println(reuslts);
Map result = (Map)xStream.fromXML(reuslts);
Iterator it = Set().iterator();
while(it.hasNext()){
Map.Entry enty = (Entry) it.next();
System.out.Key()+":"+Value());
}
}catch(Exception e){
e.printStackTrace();
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论