Click here to Skip to main content
15,887,746 members
Articles / Webservice

MEC: Call Web Service in a Cleaner Way

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
23 Jul 2016CPOL2 min read 6.8K   2  
In the MEC mapper, calling a web service is a general scenario. There are few ways to do it.

Background

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

Java
java.net.URL obj = new java.net.URL(iURL);
java.net.HttpURLConnection con = (java.net.HttpURLConnection) obj.openConnection();

con.setRequestMethod("POST");
con.setRequestProperty ("Content-Type","text/xml");
con.setRequestProperty ("Accept","text/xml");

String itemStr = "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" 
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" 
xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
			+ "<soap:Body>"
			+ "<GetItem xmlns=\"http://tempuri.org/\">"
			+ "<itemNumber>KelumG</itemNumber>"
			+ "</GetItem>"
			+ "</soap:Body></soap:Envelope>";		
con.setDoOutput(true);
java.io.DataOutputStream wr = new java.io.DataOutputStream(con.getOutputStream());
wr.writeBytes(itemStr);
wr.flush();
wr.close();

This is very primitive and takes a lot of the developer's time. Also, it hinders code readability and maintainability.

From this post, I’m going to tell you a cleaner, rather simple way. We, programmers, know an 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.

Solution

  1. Create Proxy class from the web service.
    The service I’m using for this post is http://localhost:4599/HelloService.asmx?wsdl (refer to the below P.S. section). There are few tools to create proxy classes in Java (wsimport, axis2, java2wsdl, etc.). Here, I’m using wsimport with the following argument.

    -d KG: store generated class into KG folder

    wsimport -d KG http://localhost:4599/HelloService.asmx?wsdl command to create proxy classes.

    Image 1

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

    Note
    There are limitations for customer JARs. (Page # 85, Business Document Mapper, Version 11.4.3.0)

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

    Javadoc, encoding
    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.

  3. In the map, add reference to this custom JAR file.

    Image 2

  4. Simply call the service just like a method.
    Java
    private void callWebSvc() throws Throwable {
    	// Please implement me
    	org.tempuri.HelloService hs = new org.tempuri.HelloService(new java.net.URL(iUrl));
    	org.tempuri.HelloServiceSoap hsoap= hs.getHelloServiceSoap();
    	oItem = hsoap.getItem(iItem);
    }

P.S.

For this post, I have created a mock web service (.asmx) using C# and hosted in IIS. The sample code is as below:

Java
namespace WebServiceApp
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class HelloService : System.Web.Services.WebService
    {
        [WebMethod]
        public string GetItem(string itemNumber)
        {
            return new ItemService().GetItem(itemNumber);
        }
    }

    public class ItemService
    {
        public string GetItem(string itno)
        {
            return string.Format("Item {0}",itno);
        }
    }

Image 3

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Brandix Lanka Pvt Ltd.
Sri Lanka Sri Lanka
I’ve started my career in 2001 with Microsoft .net ver 1.0. I’m a MCSD for .net.

Currently, I’m working for Sri Lanka’s largest apparel exporter as a Software Engineer. All projects in .net, MS Sql Server, Biztalk Server, WCF and WPF. And also, I’m developing components to the ERP. In addition to that, I’ve involved to create architecture of ERP integration.

Comments and Discussions

 
-- There are no messages in this forum --