JAXWS — how to change the endpoint address

JavaJax WsWebservice Client

Java Problem Overview


How can I dynamically change the address which my JAXWS client is using? This client was generated by wsimport.

Java Solutions


Solution 1 - Java

You can achieve that using the BindingProvider interface.

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();

Solution 2 - Java

Solved the problem using Apache CXF.

With just two lines of code! Here is the snippet:

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);

Solution 3 - Java

I am new to PayPal integration, i am not sure about Adaptive Payment api. But we have a privilege to check whether specific email id having account in PayPal or not using GetVerifiedStatus method.

Please use below sandbox wsdl URL for verifying email

URL : https://svcs.sandbox.paypal.com/AdaptiveAccounts?wsdl

Response will be like below

<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>

Note: while creating stub don't forgot to set endpoint as below. if we are not setting this, we can't get expected output.

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

use below method to add endpoint

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);*/
	}

Solution 4 - Java

I am not sure on how to do that if you use wsimport. I had the same issue, so I used Intellij IDEA (version 9) to create client code for me. It provided a service endpoint constructor that takes in the wsdl url.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKico LoboView Question on Stackoverflow
Solution 1 - JavaAviram SegalView Answer on Stackoverflow
Solution 2 - JavaKico LoboView Answer on Stackoverflow
Solution 3 - JavaAnand sagaView Answer on Stackoverflow
Solution 4 - Javauncaught_exceptionsView Answer on Stackoverflow