第一個webService一些問題

在網上學習看ws寫的類

package cn.dhcc.axis2;

import javax.xml.namespace.QName;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.apache.axis2.addressing.EndpointReference;


public class RPCClient {
//
private final static String SERVICEEPR = "http://localhost:8080/axis2/services/SimpleService";
private final static String NS = "http://ws.apache.org/axis2";
private static EndpointReference targetEPR = new EndpointReference(SERVICEEPR); //指定調用webserice的url
public static void main(String[] args) throws Exception{

//使用rpc方式調用ws
RPCServiceClient serivceClient = new RPCServiceClient();
Options options = serivceClient.getOptions();
options.setTo(targetEPR);

//這裏必須配置SOAPAction=http://www.example.com/OnlineBanking/Login;        
//否則就會報“org.apache.axis2.AxisFault:
        //The endpoint reference (EPR) for the Operation not found is
        //http://localhost:8088/ums/services/OnlineBankingService and the WSA Action = urn:anonOutInOp”
        //的錯誤.
        //而且必須跟WSDL裏面定義的一致
// <wsdl:operation name="getPrice">
// <soap:operation soapAction="urn:getPrice" style="document"/>
//options.setAction("urn:sayHello");

//指定要調用sayHello方法及wsdl文件的命名空間 
//targetNamespace="http://ws.apache.org/axis2"
QName opAddEntry = new QName(NS,"sayHello");
//調用sayHello方法
//param1:方法,param2:方法入參 Object[],param3:返回值類型 Class[]
System.out.println(serivceClient.invokeBlocking(opAddEntry, new Object[]{"wanghc"},new Class[]{String.class})[0]);
Thread.sleep(1000);
//getprice方法代碼
//options.setAction("urn:getPrice");
opAddEntry = new QName(NS,"gerPrice");
System.out.println(serivceClient.invokeBlocking(opAddEntry, new Object[]{},new Class[]{int.class})[0]);


}

開始報錯

2011-05-16 17:20:55,043 - < Parameter add on object org.apache.axis2.description.ParameterIncludeImpl@113ff65>
2011-05-16 17:20:55,043 - < Key =password>
2011-05-16 17:20:55,043 - < Value =axis2>
2011-05-16 17:20:55,043 - < Value Class = java.lang.String>
2011-05-16 17:20:55,043 - < Value Classloader = null>
2011-05-16 17:20:55,043 - <Call Stack = DEBUG_FRAME = org.apache.axis2.util.JavaUtils.callStackToString(JavaUtils.java:564)
DEBUG_FRAME = org.apache.axis2.description.ParameterIncludeImpl.debugParameterAdd(ParameterIncludeImpl.java:315)
DEBUG_FRAME = org.apache.axis2.description.ParameterIncludeImpl.addParameter(ParameterIncludeImpl.java:106)

.....

我用的axis2是1.4.1版本的我把他所有的jar都放到buidlpath中

後面在網上找了下,是包的問題

把包改成下面的就行了(我把jar放到我的資源中了)



後面就用問題

Exception in thread "main" org.apache.axis2.AxisFault: The endpoint reference (EPR) for the Operation not found is http://localhost:8080/axis2/services/SimpleService and the WSA Action = urn:anonOutInOp 

網上查了下發現那個

少了這句

options.setAction("urn:getPrice");

"urn:getPrice"  這個是wsdl中的

wsdl:binding下的

<wsdl:operation name="getPrice">
<soap:operation soapAction="urn:getPrice" style="document"/>
....
最後就可以成功運行了.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章