2010-09-14 102 views
4

我創建了一個簡單的web服務,它實現了一個添加操作,並使用wsimport創建了一些客戶端文件。現在我想盡可能少地包含wsdl特定的工件。下面是一個如何調用Web服務的例子:爲什麼JAX-WS需要包裝類?

String serviceNamespace = "http://jws.samples.geronimo.apache.org/"; 
String serviceName = "CalculatorServiceService"; 
QName serviceQN = new QName(serviceNamespace, serviceName); 
Service service = Service.create(new URL("http://localhost:8080/WebService/calculator?wsdl"), serviceQN); 

String portNamespace = "http://jws.samples.geronimo.apache.org/"; 
String portName = "CalculatorServicePort"; 
QName portQN = new QName(portNamespace, portName); 
Calculator myProxy = (Calculator) service.getPort(portQN, Calculator.class); 

但是好像我必須包含每個消息的包裝類。例如,加法運算的結果的消息:

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "addResponse", propOrder = { "_return" }) 
public class AddResponse { 
    @XmlElement(name = "return") 
    protected int _return; 
    public int getReturn() { 
     return _return; 
    } 
    public void setReturn(int value) { 
     this._return = value; 
    } 
} 

這些包裝被註解內應用於服務接口:

@WebService(name = "Calculator", targetNamespace = "http://jws.samples.geronimo.apache.org/") 
public interface Calculator { 
    @WebMethod 
    @RequestWrapper(className = "org.example.webservices.clients.dynamicproxy.Add") 
    @ResponseWrapper(className = "org.example.webservices.clients.dynamicproxy.AddResponse") 
    public int add(
     @WebParam(name = "value1", targetNamespace = "") 
     int value1, 
     @WebParam(name = "value2", targetNamespace = "") 
     int value2); 
} 

如果註釋被移除,所述web服務將不會運行。

com.sun.xml.ws.model.RuntimeModelerException: runtime modeler error: Wrapper class org.example.webservices.clients.dynamicproxy.jaxws.Add is not found. Have you run APT to generate them?

但是,爲什麼我需要這些包裝? JAX-WS不能即時創建這些包裝?您是否看到任何信息,這些信息無法從wsdl文件中檢索到?

+0

找到一個值得閱讀的文檔。 http://pic.dhe.ibm.com/infocenter/wasinfo/v7r0/index.jsp?topic=%2Fcom.ibm.websphere.express.doc%2Finfo%2Fexp%2Fae%2Ftwbs_jaxwsdynclient.html – kaushik 2013-03-05 03:59:18

回答

3

默認情況下,您的服務是WRAPPED而不是BARE,因此消息中的頂級項目必須是與操作具有相同名稱的類型。在'傳統'JAX-WS中,這需要你添加一個包裝類型。

如果您使用Apache CXF,它將自動生成帶有ASM的這些包裝。

1

在服務界面,如果你有一個條目:

@WebResult(targetNamespace = "http://jws.samples.geronimo.apache.org/") 

前加方法在計算器界面,那麼你就可以得到由JAX-WS客戶端上-the-創建的包裝類使用以下代碼飛行:

import java.net.URL; 

import javax.xml.namespace.QName; 

import javax.xml.ws.Service; 

public class TestWS{ 

    public static void main(String args[]) throws Exception 
    { 
    URL url = "url to wsdl" 

    QName qname = new QName("http://jws.samples.geronimo.apache.org/", "Name of your service"); 
    Service service = Service.create(url, qname); 
    Calculator calcPort = service.getPort(Calculator.class); 
    System.out.println("Result of 1+2 is " + calcPort.add(1,2));  
    } 
} 
相關問題