What is a good Java library for dynamic operations with SOAP clients?

I searched for SOAP client libraries for Java and found many libraries based on the idea of ​​creating stub classes and proxies based on WSDL. I am interested in letting the user enter WSDL at runtime, parse the WSDL, and then let the user perform operations in the web service.

Does anyone know of a good SOAP client library that will allow this runtime to be used? Or can I use the axis2 function wsdl2java to create stubs in the class loader and use them at runtime?

+6
source share
2 answers

Later than never. :)

You must achieve this in two steps:

  • 1) disassemble the user-informed WSDL to obtain available operations. Refer to this question to find out how to do this in a simple way.

  • 2) Create a dynamic client to send a request using the selected operations. This can be done using the Dispatch API from Apache CXF .

Create a Dispatch object for a dynamic client (you can create it on the fly by telling the endpoint of the web service, port name, etc.):

 package com.mycompany.demo; import javax.xml.namespace.QName; import javax.xml.ws.Service; public class Client { public static void main(String args[]) { QName serviceName = new QName("http://org.apache.cxf", "stockQuoteReporter"); Service s = Service.create(serviceName); QName portName = new QName("http://org.apache.cxf", "stockQuoteReporterPort"); Dispatch<DOMSource> dispatch = s.createDispatch(portName, DOMSource.class, Service.Mode.PAYLOAD); ... } } 

Build a request message (In the example below, we use a DOMSource ):

 // Creating a DOMSource Object for the request DocumentBuilder db = DocumentBuilderFactory.newDocumentBuilder(); Document requestDoc = db.newDocument(); Element root = requestDoc.createElementNS("http://org.apache.cxf/stockExample", "getStockPrice"); root.setNodeValue("DOW"); DOMSource request = new DOMSource(requestDoc); 

Web service call

 // Dispatch disp created previously DOMSource response = dispatch.invoke(request); 

Recommendations:

  • Use ((BindingProvider)dispatch).getRequestContext().put("thread.local.request.context", "true"); if you want the Dispatch object stream to be safe.
  • Download the Dispatch object for later use, if so. The process of creating an object is not free.

Other methods

There are other methods for creating dynamic clients, for example, using the CXF dynamic-clients API. You can read on the project index page:

CXF supports several alternatives allowing the application to communicate with the service without SEI classes and data

I have not tried it myself, but it's worth a try.

+3
source

you can do this type of functionality, but you have the same method input type and return type for the web service method.

-1
source

Source: https://habr.com/ru/post/893711/


All Articles