java调用wsdl接口示例,java中怎么调用web service的wsdl(wsdl已经发布成功,企业中如何去用这个wsdl)
java调用wsdl接口示例,java中怎么调用web service的wsdl(wsdl已经发布成功,企业中如何去用这个wsdl)详细介绍
本文目录一览: Java调用wsdl,怎么实现
步骤如下:
1.下载AXIS2类库,AXIS2是目前java调用webservice的一个主要方法(由于更新较频繁,请自行google该类库的网址)
2.由于是第三方webservice,直接引入AXIS2的包就可以
代码如下:
import java.rmi.RemoteException;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
public class webServiceTest {
public String invokeRemoteFuc() {
String endpoint = "http://localhost:8080/webservice/services/helloworld";
String result = "no result!";
Service service = new Service();//新建一个service
Call call;
Object[] object = new Object[1];
object[0] = "Dear I miss you";//Object是用来存储方法的参数
try {
call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);// 远程调用路径
call.setOperationName("say");// 调用的方法名
// 设置参数名:
call.addParameter("str1", // 参数名
XMLType.XSD_STRING,// 参数类型:String
ParameterMode.IN);// 参数模式:'IN' or 'OUT'
// 设置返回值类型:
call.setReturnType(XMLType.XSD_STRING);// 返回值类型:String
result = (String) call.invoke(object);// 远程调用
} catch (ServiceException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
return result;//返回值
}
public static void main(String[] args) {
webServiceTest t = new webServiceTest();
String result = t.invokeRemoteFuc();
System.out.println(result); //输出
}
}
通过AXIS2封装好的类设置URL和参数,直接调用。
导入axis的八个包:
axis.jar
commons-discovery.jar
commons-logging.jar
jaxrpc.jar
saaj.jar
wsdl4j.jar
axis-ant.jar
log4j-1.2.8.jar
开发客户端程序。开发客户端程序的项目中要加入axis.jar包。
文件:TestWebService.java
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
public class TestWebService {
public static void main(String[] args) {
try {
//还记得刚才的那个WebService的services.xml中的namespace吗?
String endpoint = "http://localhost:8080/uum/services/Hello.jws?wsdl";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName("GetName"); // 这里是的Hello类中的方法名(注意大小写)
call.setReturnType(XMLType.XSD_STRING); // 返回值类型。
call.addParameter("name",org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
String[] str= {"张三"};
Object[] o = str;
//Object[] o =null;//如果你的方法中没有参数,那么就这样写吧。
String hello = (String) call.invoke(o);// 做方法执行。
System.out.println(hello);
} catch (Exception e) {
System.err.println(e.toString());
}
}
}
执行这段代码,将会在命令行中输出 “hello 张三”,表示测试成功。
Axis下载地址为http://ws.apache.org/axis/
这个就是web service框架,学一下这个例子就会了
java调用wsdl的步骤如下,主要是使用第三方框架:
步骤如下:
1.下载AXIS2类库,AXIS2是目前java调用webservice的一个主要方法(由于更新较频繁,请自行google该类库的网址)
2.由于是第三方webservice,直接引入AXIS2的包就可以用了,代码如下:
import java.rmi.RemoteException;import javax.xml.rpc.ParameterMode;import javax.xml.rpc.ServiceException;import org.apache.axis.client.Call;import org.apache.axis.client.Service;import org.apache.axis.encoding.XMLType; public class webServiceTest { public String invokeRemoteFuc() { String endpoint = "http://localhost:8080/webservice/services/helloworld"; String result = "no result!"; Service service = new Service(); Call call; Object[] object = new Object[1]; object[0] = "Dear I miss you";//Object是用来存储方法的参数 try { call = (Call) service.createCall(); call.setTargetEndpointAddress(endpoint);// 远程调用路径 call.setOperationName("say");// 调用的方法名 // 设置参数名: call.addParameter("str1", // 参数名 XMLType.XSD_STRING,// 参数类型:String ParameterMode.IN);// 参数模式:'IN' or 'OUT' // 设置返回值类型: call.setReturnType(XMLType.XSD_STRING);// 返回值类型:String result = (String) call.invoke(object);// 远程调用 } catch (ServiceException e) { e.printStackTrace(); } catch (RemoteException e) { e.printStackTrace(); } return result; } public static void main(String[] args) { webServiceTest t = new webServiceTest(); String result = t.invokeRemoteFuc(); System.out.println(result); }}该方法的原理很简单,通过AXIS2封装好的类设置URL和参数,直接调用就好了,我们要关注的就是设置URL,方法,还有方法的参数,其他的copy&paste好啦,很简单吧,再看看其他的方法,我勒个去了,害我瞎搞两天。迟点上个源码共大家参考!
java程序如何调用webservice接口,实现发送短信功能
给你一个最简单的方法:
第一、根据http://134.224.102.6:80/CompanySendSmInf/services/SmsInf?wsdl 拿到WSDL文件。
第二、根据Axis的jar包,把WSDL文件生成客服端java代码。(可以把java文件打成jar文件,便于管理。怎么生成java代码,百度里都有说明我就不写了。)
第三、在你工程里用AXIS的功能属性,调用外部接口;给你一个格式模板:
MobileCodeWSLocator l=new MobileCodeWSLocator();//MobileCodeWSLocator是WSDL文件生成客服端java类;
MobileCodeWSSoap s=l.getMobileCodeWSSoap();();//MobileCodeWSSoap 是WSDL文件生成客服端java类
String m=s.getMobileCodeInfo("13800", "");
如果你用Axis生成的java类,格式和上面一样;自己参考一下就懂了。
你上面明显的连接异常,第三方服务明显没有开,WEBSERVICE可以设置户名、密码,像我们行所有的WEBSERVICE都设置,安全考虑吧。还有不懂的可以call我。
如何调WSDL方式的webservice
下面我们来看Java如何通过WSDL文件来调用这些web service: 注意,以下的代码并没有经过真正的测试,只是说明这些情况,不同版本的Axis相差很大,大家最好以apache网站上的例子为准,这里仅仅用于说明其基本用法。 1,直接AXIS调用远程的web service 我觉得这种方法比较适合那些高手,他们能直接看懂XML格式的WSDL文件,我自己是看不懂的,尤其我不是专门搞这行的,即使一段时间看懂,后来也就忘记了。直接调用模式如下:
import java.util.Date;import java.text.DateFormat;import org.apache.axis.client.Call;import org.apache.axis.client.Service;import javax.xml.namespace.QName;import java.lang.Integer;import javax.xml.rpc.ParameterMode; public class caClient { public static void main(String[] args) { try { String endpoint = "http://localhost:8080/ca3/services/caSynrochnized?wsdl"; //直接引用远程的wsdl文件 //以下都是套路 Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(endpoint); call.setOperationName("addUser");//WSDL里面描述的接口名称 call.addParameter("userName", org.apache.axis.encoding.XMLType.XSD_DATE, javax.xml.rpc.ParameterMode.IN);//接口的参数 call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型 String temp = "测试人员"; String result = (String)call.invoke(new Object[]{temp}); //给方法传递参数,并且调用方法 System.out.println("result is "+result); } catch (Exception e) { System.err.println(e.toString()); } }}2,直接SOAP调用远程的webservice 这种模式我从来没有见过,也没有试过,但是网络上有人贴出来,我也转过来
import org.apache.soap.util.xml.*;import org.apache.soap.*;import org.apache.soap.rpc.*; import java.io.*;import java.net.*;import java.util.Vector; public class caService{ public static String getService(String user) { URL url = null; try { url=new URL("http://192.168.0.100:8080/ca3/services/caSynrochnized"); } catch (MalformedURLException mue) { return mue.getMessage(); } // This is the main SOAP object Call soapCall = new Call(); // Use SOAP encoding soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC); // This is the remote object we're asking for the price soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized"); // This is the name of the method on the above object soapCall.setMethodName("getUser"); // We need to send the ISBN number as an input parameter to the method Vector soapParams = new Vector(); // name, type, value, encoding style Parameter isbnParam = new Parameter("userName", String.class, user, null); soapParams.addElement(isbnParam); soapCall.setParams(soapParams); try { // Invoke the remote method on the object Response soapResponse = soapCall.invoke(url,""); // Check to see if there is an error, return "N/A" if (soapResponse.generatedFault()) { Fault fault = soapResponse.getFault(); String f = fault.getFaultString(); return f; } else { // read result Parameter soapResult = soapResponse.getReturnValue (); // get a string from the result return soapResult.getValue().toString(); } } catch (SOAPException se) { return se.getMessage(); } }}
java如何调用webservice接口
Java通过WSDL文件来调用webservice:
注意,以下的代码并没有经过真正的测试,只是说明这些情况,不同版本的Axis相差很大,大家最好以apache网站上的例子为准,这里仅仅用于说明其基本用法。
1,直接AXIS调用远程的web service
这种方法比较适合那些高手,他们能直接看懂XML格式的WSDL文件,我自己是看不懂的,尤其我不是专门搞这行的,即使一段时间看懂,后来也就忘记了。直接调用模式如下:
import java.util.Date;
import java.text.DateFormat;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
import java.lang.Integer;
import javax.xml.rpc.ParameterMode;
public class caClient {
public static void main(String[] args) {
try {
String endpoint = "http://localhost:8080/ca3/services/caSynrochnized?wsdl";
//直接引用远程的wsdl文件
//以下都是套路
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);
call.setOperationName("addUser");//WSDL里面描述的接口名称
call.addParameter("userName", org.apache.axis.encoding.XMLType.XSD_DATE,
javax.xml.rpc.ParameterMode.IN);//接口的参数
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型
String temp = "测试人员";
String result = (String)call.invoke(new Object[]{temp});
//给方法传递参数,并且调用方法
System.out.println("result is "+result);
}
catch (Exception e) {
System.err.println(e.toString());
}
}
}
2,直接SOAP调用远程的webservice
这种模式我从来没有见过,也没有试过,但是网络上有人贴出来,我也转过来
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.rpc.*;
import java.io.*;
import java.net.*;
import java.util.Vector;
public class caService{
public static String getService(String user) {
URL url = null;
try {
url=new URL("http://192.168.0.100:8080/ca3/services/caSynrochnized");
} catch (MalformedURLException mue) {
return mue.getMessage();
}
// This is the main SOAP object
Call soapCall = new Call();
// Use SOAP encoding
soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
// This is the remote object we're asking for the price
soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");
// This is the name of the method on the above object
soapCall.setMethodName("getUser");
// We need to send the ISBN number as an input parameter to the method
Vector soapParams = new Vector();
// name, type, value, encoding style
Parameter isbnParam = new Parameter("userName", String.class, user, null);
soapParams.addElement(isbnParam);
soapCall.setParams(soapParams);
try {
// Invoke the remote method on the object
Response soapResponse = soapCall.invoke(url,"");
// Check to see if there is an error, return "N/A"
if (soapResponse.generatedFault()) {
Fault fault = soapResponse.getFault();
String f = fault.getFaultString();
return f;
} else {
// read result
Parameter soapResult = soapResponse.getReturnValue ();
// get a string from the result
return soapResult.getValue().toString();
}
} catch (SOAPException se) {
return se.getMessage();
}
}
}
3,使用wsdl2java把WSDL文件转成本地类,然后像本地类一样使用,即可。
这是像我这种懒人最喜欢的方式,仍然以前面的global weather report为例。
首先 java org.apache.axis.wsdl.WSDL2Java http://www.webservicex.net/globalweather.asmx.WSDL
原本的网址是http://www.webservicex.net/globalweather.asmx?WSDL,中间个各问号,但是Linux下面它不能解析,所以去掉问号,改为点号。
那么就会出现4个文件:
GlobalWeather.java GlobalWeatherLocator.java GlobalWeatherSoap.java GlobalWeatherSoapStub.java
其中GlobalWeatherSoap.java是我们最为关心的接口文件,如果你对RMI等SOAP实现的具体细节不感兴趣,那么你只需要看接口文件即可,在使用的时候,引入这个接口即可,就好像使用本地类一样。
Java通过WSDL文件来调用webservice直接调用模式如下:
import java.util.Date;
import java.text.DateFormat;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
import java.lang.Integer;
import javax.xml.rpc.ParameterMode;
public class caClient {
public static void main(String[] args) {
try {
String endpoint = "http://localhost:8080/ca3/services/caSynrochnized?wsdl";
//直接引用远程的wsdl文件
//以下都是套路
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);
call.setOperationName("addUser");//WSDL里面描述的接口名称
call.addParameter("userName", org.apache.axis.encoding.XMLType.XSD_DATE,
javax.xml.rpc.ParameterMode.IN);//接口的参数
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型
String temp = "测试人员";
String result = (String)call.invoke(new Object[]{temp});
//给方法传递参数,并且调用方法
System.out.println("result is "+result);
}
catch (Exception e) {
System.err.println(e.toString());
}
}
}
http://www.blogjava.net/zjhiphop/archive/2009/04/29/webservice.html
有几种方法
Java调用WebService可以直接使用Apache提供的axis.jar自己编写代码,或者利用Eclipse自动生成WebService Client代码,利用其中的Proxy类进行调用。理论上是一样的,只不过用Eclipse自动生成代码省事些。
1、编写代码方式:
package com.yudun.test;
import java.rmi.RemoteException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.message.PrefixedQName;
import org.apache.axis.message.SOAPHeaderElement;
import com.cezanne.golden.user.Exception;
import com.cezanne.golden.user.UserManagerServiceProxy;
import javax.xml.namespace.QName;
import java.net.MalformedURLException;
import javax.xml.rpc.ServiceException;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPException;
public class testWebService {
public static String getResult() throws ServiceException, MalformedURLException, RemoteException, SOAPException
{
//标识Web Service的具体路径
String endpoint = "WebService服务地址";
// 创建 Service实例
Service service = new Service();
// 通过Service实例创建Call的实例
Call call = (Call) service.createCall();
//将Web Service的服务路径加入到call实例之中.
call.setTargetEndpointAddress( new java.net.URL(endpoint) );//为Call设置服务的位置
// 由于需要认证,故需要设置调用的SOAP头信息。
Name headerName = new PrefixedQName( new QName("发布的wsdl里的targetNamespace里的url", "string_itemName") );
org.apache.axis.message.SOAPHeaderElement header = new SOAPHeaderElement(headerName);
header.addTextNode( "blablabla" );
call.addHeader(header);
// SOAPHeaderElement soapHeaderElement = new SOAPHeaderElement("发布的wsdl里的targetNamespace里的url", "SoapHeader");
// soapHeaderElement.setNamespaceURI("发布的wsdl里的targetNamespace里的url");
// try
// {
// soapHeaderElement.addChildElement("string_itemName").setValue("blablabla");
// }
// catch (SOAPException e)
// {
// e.printStackTrace();
// }
// call.addHeader(soapHeaderElement);
//调用Web Service的方法
org.apache.axis.description.OperationDesc oper;
org.apache.axis.description.ParameterDesc param;
oper = new org.apache.axis.description.OperationDesc();
oper.setName("opName");
param = new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("", "arg0"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"), java.lang.String.class, false, false);
param.setOmittable(true);
oper.addParameter(param);
param = new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("", "arg1"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"), java.lang.String.class, false, false);
param.setOmittable(true);
oper.addParameter(param);
param = new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("", "arg2"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"), java.lang.String.class, false, false);
param.setOmittable(true);
oper.addParameter(param);
oper.setReturnType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"));
oper.setReturnClass(java.lang.String.class);
oper.setReturnQName(new javax.xml.namespace.QName("", "return"));
oper.setStyle(org.apache.axis.constants.Style.WRAPPED);
oper.setUse(org.apache.axis.constants.Use.LITERAL);
oper.addFault(new org.apache.axis.description.FaultDesc(
new javax.xml.namespace.QName("发布的wsdl里的targetNamespace里的url", "Exception"),
"Exception",
new javax.xml.namespace.QName("发布的wsdl里的targetNamespace里的url", "Exception"),
true
));
call.setOperation( oper );
call.setOperationName(new javax.xml.namespace.QName("发布的wsdl里的targetNamespace里的url", "opName"));
//调用Web Service,传入参数
String res = ( String ) call.invoke( new Object[]("arg0","arg1"));
System.out.println("===============");
return res;
}
/**
* @param args
*/
public static void main(String[] args) {
try {
System.out.println(getResult());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
} catch (SOAPException e) {
e.printStackTrace();
}
}
}
2、利用Eclipse自动生成WebService client代码就容易多了:(由于还不会发图片,就用语言描述了,大家酬和看吧。。。)
首先,new project,选择other,在输入框中输入Web Service Client,选中搜索后的结果,点击Next,在Service definition中输入 WebService的发布地址,点击Finish
这样,WebService Client代码已经生成好了。
接下来写一个Test类,在main函数中输入如下代码:
String endpoint = "服务器的WebService的地址";
YourWebServiceNameProxy umsp = new YourWebServiceNameProxy (endpoint);
try {
String resultStr = umsp.opMethod("arg0","arg1");
System.out.println(resultStr);
} catch (Exception e) {
System.out.println("异常");
e.printStackTrace();
} catch (RemoteException e) {
System.out.println("RemoteException异常");
e.printStackTrace();
}
如果还有疑问的话还有视频,如果对你有帮助请采纳!
java2wsdl生成的客户端代码怎么调用
首先, 你要先把你的WS服务启动起来,就是 比如ht tp:/ /localhost:8080/Example/services/HelloWorldService?wsdl
然后在你的另一个项目中建一个Webservice Client 客户端,用来访问你的WS服务。
建立Webservice Client 方法如下,在Eclipses中建立一个java工程,然后在src上右键--NEW---Other---Web Service Client --Xfire--在WsdL url 中写上htt p:/ /localhost:8080/Example/services/HelloWorldService?wsdl-----下一步结束。
在src里的会出现一些java文件,你找一个以Client结束的java文件,在里面的main方法中会有个service对象,现在你就可以直接用这个对象了,service.peerstatus(参数)这样写就行了。
java中怎么调用web service的wsdl(wsdl已经发布成功,企业中如何去用这个wsdl)
Client client = new Client(new URL(url)); Object results[] = client.invoke(method, paramArray);
第一步是建立连接,第二步就是调用指定的方法,并传入指定的参数。 返回指定的结果。
上面是我自己用到的调用,具体的参数类型和返回类型由你的wsdl指定。
希望对你有帮助,望采纳
关于Webservice接口的Java客户端调用
String endpoint="http://localhost:8080/xxx/services/userservice?wsdl";
String id = "11111";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new URL(endpoint));
call.setOperationName("webservice方法名");
String res = (String) call.invoke(new Object[] {id});
看了你的描述觉得你把webservice想得太复杂化了,其实就是一个jar包和几个类。
以上就是最简单的webservice客户端用法,和反射有点像。当然返回值不一定是String,返回的类型和格式要问服务提供方。
我用的是axis的,我不了解websphere什么的,但是webservice就是那么易用的东西。
高分求java实现SAOP、WSDl用法例子(最好是电信与sp之间通信的例子)
当我们用web service对外提供接口服务的时候,可能很多接口会返回复杂类型,比如数组; 在此次利用.Net下面WCF和我们ASSP API【Alisoft SaaS Platform API】进行联调的时候,就发现了问题;如果要想让通过WSDL发布的web service具有最大化的兼容性,最好是参考WS-I【Web Services Interoperability】标准,而不要仅仅局限于SOAP标准;以下就返回数组的问题进行下探讨;
ASSP API接口之一为getUsingUser,返回的对象为数组,数组元素也为复杂对象SimpleUserInfo,其中包括两个属性userId和userName;在WSDL声明此type的时候默认采用的是soapenc:Array的方式,这也是SOAP规范中的方式;其声明部分如下:
该声明方式在和大部分的客户端语言联调时没有问题,比如Java、.Net WSE 2.0、.Net WSE 3.0、Php;但是在利用WCF进行联调时就出现了问题,其表现为:1)在vs2008中通过Add Service Reference添加服务时不成功,生成的代理类为空;2)若通过命令行工具Svcutil生成,提示不成功,但能看到错误信息,提示如下:
System.ServiceModel.Description.XmlSerializerMessageContractImporter
错误: 引用类型“http://schemas.xmlsoap.org/soap/encoding/:Array”仅对编码的SOAP 有效。
经查,出现以上问题就是由于soapenc:Array的声明方式所导致,SOAP规范中的这种方式兼容性很不好,这点在WS-I BP【WS-I Basic Profile】中有过重点说明;后修改成unbounded模式后测试通过,修改后的声明方式为:
soapenc:Array的问题解决之后,WCF环境下的联调并没有完全成功,还差一点点;WSDL声明修改之后,vs2008中添加服务引用正常了,调用服务端ws也正常了,在responseXml中返回了正常的结果,但是WCF对responseXml解析出来的结果却有问题,userId为null,userName正常,难道userId的信息丢掉了!后经MS人员查证之后是由于WSDL中声明SimpleUserInfo中属性信息和soap responseXml中属性信息的先后顺序不一致所导致!为了不让ISV修改客户端生成的代码,所以只好修改了服务端WSDL中userId、userName两者的顺序,之后问题全部解决;
总结下,虽然说web service说是完全跨平台的,但在实际开发过程中还是会出现很多的不兼容问题,这不仅仅是由于开发人员的代码编写上,连某些标准规范都是有瑕疵的,并不是说遵循了SOAP标准规范就没有任何问题了,至于这点,大家可以多参考WS-I标准,他里面指出了很多能够保证最大化兼容性的措施和方法,这会非常有用;
webservices java 实现wsdl 接口
call.setOperationName(new QName("getData")); //设置要调用的接口方法//这样写试试,另外如果参数传不进去可以这样写//设置参数名 第二个参数表示String类型,第三个参数表示入参call.addParameter("user", org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);