Dynamic WS Clients. Eureka! 2. How-to in Java
0. Introduction
In the last post we got information about:- Fully qualified names of classes
- The structures (classes) used in the WS for input and output.
1. The pom.xml file
It is the same as the one in this elder post. The important thing is using Apache CXF libraries ("cxf-rt-frontent-jaxws", "cxf-rt-transports-http" and "cxf-rt-transports-http-jetty")
2. Simple scaffolding(1): Accessing the WSDL
This is as simple as this 2 java lines of code:
// 0.Package declarations import org.apache.cxf.endpoint.Client; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; ... // 1. Declare WSDL JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); Client client = dcf.createClient("http://XXX.XXX.XX.XX:8080/gexflow/ws/Registro_v2.0?wsdl");
Here the "XXX.XXX.XX.XX" represents the WS server IP address.
Here is the simple code. We only need the fully qualified class names. A tool class for creating classes is needed (a class loader). For instance, for the input class, we can create the class and an instance dynamically this way (remember that its fully qualified name is "gexflow.reges.RegistrarAsientoEntrada") :
3. Simple scaffolding(2): Creating the WS needed classes and instances dynamically
Here is the simple code. We only need the fully qualified class names. A tool class for creating classes is needed (a class loader). For instance, for the input class, we can create the class and an instance dynamically this way (remember that its fully qualified name is "gexflow.reges.RegistrarAsientoEntrada") :
// 1. Class tool
ClassLoader classLoader=Thread.currentThread().getContextClassLoader();
// 2. Defining class and instance
Class<?> regEntInputClass=null; Object regEntInput = null; // 3. Create input class and instance dynamically try { regEntInputClass=classLoader.loadClass("gexflow.reges.RegistrarAsientoEntrada"); regEntInput = regEntInputClass.getConstructor().newInstance(); } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | ClassNotFoundException e1) { e1.printStackTrace(); }
4. Simple scaffolding(3): Setting attribute values of generated instances.
As the compiler does not know what is the class structure, we need to use this Apache Commons FieldUtils class to set attributes. In this simple case, we are filling the attribute "idEntidad" from instance object "regEntInput" the integer value of "1",
try {
// Assign a value to an attribute of a class org.apache.commons.lang3.reflect.FieldUtils.writeField(regEntInput, "idEntidad", 1 , true); .... } catch (Exception e ) { e.printStackTrace(); }
5. Simple scaffolding(4): Invoking the operation from the WS
As simple as invoking the service by "client.invokeWrapped" and passing the name of the operation ("registrarAsientoEntrada) and the input instance (regEntInput) whose values have been assigned in the previously.
// Output object (obtained by invoking the WS operation) Object[] res=null; // Operation name from the WSDL String operationWS="registrarAsientoEntrada"; try { // WS invocation with operaion name and input structure res = client.invokeWrapped(operationWS,regEntInput); } catch (Exception e) { e.printStackTrace(); }
Now it's time to analyze the array object "res" to see if we have achieved our goal and analyze also if an exception has been risen for correcting possible mistakes.
Now it seems more easy for me. Happy codding!
Now it seems more easy for me. Happy codding!
Comments
Post a Comment