2011-03-22 72 views
2

通行證對象web服務

一個Web服務:

package axis2; 
public class Number { 
    public numb getNumber(numb nr){ 
     return nr; 
} 
} 

類麻木:

package axis2; 
public class numb { 
    private int val; 

    public numb(int val) 
    { 
      this.val = val; 
    } 

    public int getVal() { 
      return val; 
    } 

    public void setVal(int val) { 
      this.val= val; 
    } 
} 

我怎麼能作爲參數傳遞給服務對象,並返回一個對象?

package axis2; 
import java.rmi.RemoteException; 
import axis2.NumberStub.GetNumberResponse; 
public class ServiceTest { 
    public static void main(String[] args) throws RemoteException { 
     numb nr=new numb(4); 
     NumberStub stub = new NumberStub(); 
     GetNumberResponse res = stub.getNumber(nr); 
     System.out.println(res.get_return()); 
     } 
} 

我得到一個錯誤:

The method getNumber(NumberStub.GetNumber) in the type NumberStub is not applicable for the arguments (numb) 

當我嘗試這一點。

回答

0

只是傳遞/返回一個原語比較容易 - 在你的情況下,傳遞/返回一個int,並在內部使用numb。即:

public class Number { 
    public int getNumber(int nr){ 
     numb n = new numb(nr); 
     // do your thing here... 
     return n.getVal (); 
    } 
} 

也就是說,如果你真的想傳遞對象,一個選擇是尋找到JAXB。基本上,使用JAXB,您可以將對象放入/取出XML,也就是說,您的服務將接受numb的XML表示形式,並返回結果的XML表示形式。

請注意,您遇到的問題是您致電stub.getNumber(nr);。我在收集你的NumberStub類沒有一個方法getNumber,它需要一個numb作爲參數。