MEC: Call Web Service in a cleaner way
In the MEC mapper, calling a web service is a general scenario. There are few ways to do it. One method is build the SOAP request Payload in a string and submit via POST method. (as below code snippet)
This is very primitive and takes lot of times of developer. Also it hinders the code readability & maintainability.
From this post I’m going to tell you a cleaner way, rather simple way. We, programmers, know easy way to call web service is using Proxy objects/classes.
Proxy objects are type of representations of the web service for the programming language we are using.
- Create Proxy class from the web service.
The service I’m using for this post is http://localhost:4599/HelloService.asmx?wsdl (refer below P.S. section). There are few tools to create proxy classes in java (wsimport, axis2, java2wsdl etc. ). Here I’m using wsimport with following argument.
-d KG : store generated class into KG folder
wsimport -d KG http://localhost:4599/HelloService.asmx?wsdlcommand to create proxy classes.
- Create jar file for the generated files.
Using command line: https://docs.oracle.com/javase/tutorial/deployment/jar/build.html
Using Eclipse: http://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-33.htm
There are limitations for customer JARs. ( Page # 85, Business Document Mapper, Version 18.104.22.168)
To use a Java class from a Custom JAR in a mapping, the class has to use a package. The default package, that is no package, will not work. Note that you have to enter this package for all references to the custom class in the code for Java functions within a mapping.
If you want to get Javadoc in the mapping Java editor when using a Java class from a Custom JAR, the Java source code, that is, the .java file, must be included in the Custom JAR. The .java file must use the character encoding UTF-16, otherwise no Javadoc will be shown in the mapper. This is because the files for the mapping use that encoding.
- In the map add reference to this custom JAR file.
- Simply call the service just like a method.
For this post I have created a mock web service (.asmx ) using C# and hosted in IIS. Sample code as below.