2011-03-01 122 views
41

如何動態更改我的JAXWS客戶端使用的地址? 此客戶端由wsimport生成。JAXWS - 如何更改端點地址

+0

見http://stackoverflow.com/questions/649019/how-do-i-specify-host-and-port-when-accessing- a-web-service-from-jax-ws-generated –

+0

*動態*表示*在運行時*? –

+1

請參閱http://stackoverflow.com/questions/3569075/jaxws-service-client – McDowell

回答

9

使用Apache CXF解決了這個問題。

只有兩行代碼!以下是代碼片段:

URL url_wsdl = new URL("http://myserver/myservice?wsdl"); 
Service service = Service.create(url_wsdl, new QName("http://myaddress...", "ServiceName")); 
return service.getPort(MyJAXWSPortClass.class); 
+0

這確實/應該工作,但是構建像這樣的新服務對象不是一個好的解決方案。它會導致WSDL從新端點重新分析,並且執行起來非常昂貴。 @麥克道威爾的引用帖子是做它被問到的最好的方法:http://stackoverflow.com/questions/3569075/jaxws-service-client –

+0

@HelterScelter:同意,但在某些情況下,這個價格是可以忽略的,例如當應用程序只需要在啓動時映射此端口一次。 –

+0

它只在服務器「http:// myserver/myservice?wsdl」在線時才起作用。 如果你有幾個應用程序,這意味着按照直接定義的順序部署它們,這可能是一個問題(甚至在循環依賴的情況下也是不可能的) – GKislin

0

我不確定如何使用wsimport。我有同樣的問題,所以我使用Intellij IDEA(版本9)爲我創建客戶端代碼。它提供了一個接受wsdl url的服務端點構造函數。

+0

我正在尋找使用wsimport的解決方案。無法使用Intellij IDEA –

82

您可以使用BindingProvider接口來實現該功能。

JAX-WS custom endpoint

/** 
* The following snippets shows how to set a custom endpoint for a JAX-WS generated WebClient on runtime 
*/ 

// Get the service and the port 
SampleService service = new SampleService(); 
Sample port = service.getESamplePort(); 

// Use the BindingProvider's context to set the endpoint 
BindingProvider bp = (BindingProvider)port; 
bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://www.aviramsegal.com/ws/sample"); 

/* Optional credentials */ 
bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "user"); 
bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "password"); 
port.callSampleMethod(); 
+0

是否可以設置整個服務的端點地址,而不是每個方法的端點地址? – T3rm1

+1

服務發生變化,緩存端口對象並儘可能多地使用它 –

+0

這正是我喜歡的,因爲有時wsdl在服務器端不可用。 – havexz

0

我是新來的PayPal的整合,我不知道關於自適應支付API。 但我們有權檢查是否在PayPal中使用GetVerifiedStatus方法的特定電子郵件ID。

請使用以下沙箱WSDL URL驗證電子郵件

網址:https://svcs.sandbox.paypal.com/AdaptiveAccounts?wsdl

響應會像下面

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <ns2:GetVerifiedStatusResponse xmlns:ns2="http://svcs.paypal.com/types/aa"> 
     <responseEnvelope> 
      <timestamp>2015-07-20T23:42:46.661-07:00</timestamp> 
      <ack>Success</ack> 
      <correlationId>5cea9a8575ab9</correlationId> 
      <build>17345626</build> 
     </responseEnvelope> 
     <accountStatus>UNVERIFIED</accountStatus> 
     <countryCode>IN</countryCode> 
     <userInfo> 
      <emailAddress>[email protected]</emailAddress> 
      <accountType>PERSONAL</accountType> 
      <accountId>6KD7EVWM2E2AQW</accountId> 
      <name> 
       <salutation/> 
       <firstName>anand</firstName> 
       <middleName/> 
       <lastName>anand</lastName> 
       <suffix/> 
      </name> 
      <businessName/> 
     </userInfo> 
     </ns2:GetVerifiedStatusResponse> 
    </soapenv:Body> 
</soapenv:Envelope> 

注:創建存根不要忘了如下設置終點。 如果我們沒有設置這個,我們不能得到預期的輸出。以下方法

String endpointURL = "https://svcs.sandbox.paypal.com/AdaptiveAccounts/GetVerifiedStatus"; 

使用添加端點

private static void addEndPoint(AdaptiveAccountsPortType port, 
      String endpointURL) { 
     BindingProvider bp = (BindingProvider)port; 
     bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointURL); 

     /*List hchain = bp.getBinding().getHandlerChain(); 
     if (hchain == null) { 
      hchain = new ArrayList(); 
     } 
     hchain.add(new HTTPUserAgentHandler()); 
     bp.getBinding().setHandlerChain(hchain);*/ 
    }