Thursday, December 11, 2008

Web Service Client in a J2EE application, using JAX-WS

In my J2EE application, I use an external web service. I have generated a client of this WS using the JAX-WS tool wsimport. Constraints:
  • This web service won’t always be up.
  • As my web service client has been generated from the WSDL, I do not want each invocation of the web service to go over the network to accesses the WSDL.
  • The URL of the WS must be obtained from the configuration.
Use of the WS client in an EJB 3.0 I use the standard J2EE 5.0 dependency injection: @WebServiceRef private MyWSService myWSService; Use of a local copy of the WSDL The WSDL I used to generate the client is copied in the client’s package. The static constructor of the client is then changed to this: static { URL url = null; url = webservice.client. MyWSService.class.getResource("/webservice/client/MyWSService.wsdl"); WSDL_LOCATION = url; } This solution is suggested on this post. I tried to use OASIS catalogs but it seems that they are not supported by JBoss 4.2.2.GA. I tried another solution proposed here. The example 2.11.3 doesn’t work because the following line of code generates a “Malformed URL” exception: url = new URL(baseUrl, "../Soap11MtomUtf8.svc.xml"); Change of the target WebService URL at runtime MyWS myWS = myWSService.getMyWSPort(); ((BindingProvider) myWS).getRequestContext().put( BindingProvider.ENDPOINT_ADDRESS_PROPERTY,getURL()); response = myWS.sayHello();

No comments: