java通过wsdl调用webService查看返回参数
发布时间:2025-05-25 09:33:11 发布人:远客网络
一、java通过wsdl调用webService查看返回参数
可以查看这个,看一下是不是你要的答案:
你可以根据wsdl的Operation的名字获取所需要参数,即在浏览器的地址栏输入
web service URL/operationName/parameterName/parameterValue
就样就可以调用服务,从而获得返回的SOAP文件。这个一般都是用来测试用的;
如果是用程序来调用的话,用wsdl生成桩代码再调用服务的。不同的编程语言生成桩代码方式不一样,调用的的代码也各不相同
二、java如何调用webservice接口
Java调用WebService可以直接使用Apache提供的axis.jar自己编写代码,或者利用Eclipse自动生成WebService Client代码,利用其中的Proxy类进行调用。理论上是一样的,只不过用Eclipse自动生成代码省事些。
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.SOAPException;
public static String getResult() throws ServiceException, MalformedURLException, RemoteException, SOAPException
String endpoint="WebService服务地址";
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");
// SOAPHeaderElement soapHeaderElement= new SOAPHeaderElement("发布的wsdl里的targetNamespace里的url","SoapHeader");
// soapHeaderElement.setNamespaceURI("发布的wsdl里的targetNamespace里的url");
// soapHeaderElement.addChildElement("string_itemName").setValue("blablabla");
// call.addHeader(soapHeaderElement);
org.apache.axis.description.OperationDesc oper;
org.apache.axis.description.ParameterDesc param;
oper= new org.apache.axis.description.OperationDesc();
param= new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("","arg0"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName("","string"), java.lang.String.class, false, false);
param= new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("","arg1"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName("","string"), java.lang.String.class, false, false);
param= new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("","arg2"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName("","string"), java.lang.String.class, false, false);
oper.setReturnType(new javax.xml.namespace.QName("","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"),
new javax.xml.namespace.QName("发布的wsdl里的targetNamespace里的url","Exception"),
call.setOperationName(new javax.xml.namespace.QName("发布的wsdl里的targetNamespace里的url","opName"));
String res=( String) call.invoke( new Object[]("arg0","arg1"));
System.out.println("===============");
public static void main(String[] args){
System.out.println(getResult());
} catch(MalformedURLException e){
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);
String resultStr= umsp.opMethod("arg0","arg1");
System.out.println(resultStr);
System.out.println("RemoteException异常");
如果还有疑问的话还有视频,如果对你有帮助请采纳!
三、java调用webservice接口
java怎么调用webservice接口呢?不知道的小伙伴来看看小编今天的分享吧!
java调用webservice接口有三种方法。
方法一:直接AXIS调用远程的web service,输入代码:
public void doSelectRiskReportForm(HttpServletRequest request,
HttpServletResponse response){
//方法一:直接AXIS调用远程的web service
String endpoint= ;
Service service= new Service();
Call call=(Call) service.createCall();
call.setTargetEndpointAddress(endpoint);
String parametersName= settle_num;//参数名//对应的是 public String printWord(@WebParam(name= settle_num) String settle_num);
call.setOperationName(printWord);//调用的方法名//当这种调用不到的时候,可以使用下面的,加入命名空间名
call.setOperationName(new QName(, printWord));//调用的方法名
call.addParameter(parametersName, XMLType.XSD_STRING, ParameterMode.IN);//参数名//XSD_STRING:String类型//.IN入参
call.setReturnType(XMLType.XSD_STRING);//返回值类型:String
String result=(String) call.invoke(new Object[]{ message});//远程调用
System.out.println(result is+ result);
System.err.println(e.toString());
方法二:直接SOAP调用远程的webservice
下载jar,SOAP使用 HTTP传送 XML,尽管HTTP不是有效率的通讯协议,而且 XML还需要额外的文件解析(parse),两者使得交易的速度大大低于其它方案。但是XML是一个开放、健全、有语义的讯息机制,而 HTTP是一个广泛又能避免许多关于防火墙的问题,从而使SOAP得到了广泛的应用。但是如果效率对你来说很重要,那么你应该多考虑其它的方式,而不要用 SOAP。
import org.apache.soap.util.xml.*;
public static String getService(String user){
);
} catch(MalformedURLException mue){
// This is the main SOAP object
soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
// This is the remote object were 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,
soapParams.addElement(isbnParam);
soapCall.setParams(soapParams);
// 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();
Parameter soapResult= soapResponse.getReturnValue();
// get a string from the result
return soapResult.getValue().toString();
方法三:直接使用eclipse生成客户端.idea类同
s:element ref=s:schema/ s:any/
s:any minOccurs=2 maxOccurs=2/
然后将文件另存为weather.wsdl。
打开保存的文件路径输入cmd,输入
显示以上内容,即为生成成功,以下这是生成的文件
新建一个测试类WebserviceTest.java:
public static void main(String[] args){
//也可以使用new WeatherWebService(url)此方法可重新设置请求的地址 URL url=new URL()
WeatherWebService factory= new WeatherWebService();
WeatherWebServiceSoap weatherWebServiceSoap= factory.getWeatherWebServiceSoap();//WeatherWebServiceSoap为调用的实现类
strArray= weatherWebServiceSoap.getWeatherbyCityName(武汉);
System.out.println(strArray.getString());