{"id":313,"date":"2012-10-13T17:09:48","date_gmt":"2012-10-13T11:39:48","guid":{"rendered":"http:\/\/itechgenie.com\/myblog\/?p=313"},"modified":"2012-10-13T17:09:48","modified_gmt":"2012-10-13T11:39:48","slug":"create-web-services-using-axis-java2wsdl-wsdl2java-and-eclipse-for-all-servers-manually-part-2","status":"publish","type":"post","link":"https:\/\/itechgenie.com\/myblog\/2012\/10\/create-web-services-using-axis-java2wsdl-wsdl2java-and-eclipse-for-all-servers-manually-part-2\/","title":{"rendered":"Create Web Services using Axis Java2WSDL, WSDL2Java and Eclipse for all Servers manually &#8211; Part 2"},"content":{"rendered":"<p>With all the basic configurations done <a title=\"Create Web Services using Axis Java2WSDL, WSDL2Java and Eclipse for all Servers manually \u2013 Part 1\" href=\"http:\/\/itechgenie.com\/myblog\/2012\/10\/create-web-services-using-axis-java2wsdl-wsdl2java-and-eclipse-for-all-servers-manually-part-1\/\">as specified in the last Article<\/a> we continue to develop the Business logic.<\/p>\n<ol start=\"6\">\n<li>Create a class named Calculator.java, place four public methods add, subtract, multiply and delete and place the appropriate logics in it.\n<pre class=\"lang:java decode:true\" title=\"Calculator.java\">package com.itechgenie.services.impl;\npublic class Calculator {\n\tpublic int add(int a, int b) {\n\t\treturn a+b  ;\n\t}\n\n\tpublic int subtract(int a, int b) {\n\t\treturn a-b ;\n\t}\n\n\tpublic int multiply(int a, int b) {\n\t\treturn a * b ;\n\t}\n\n\tpublic int divide(int a, int b) throws ArithmeticException {\n\t\treturn a \/b ;\n\t}\n}<\/pre>\n<\/li>\n<li>This is the class that has to be exposed as the Web Service and we write the funky TestRunner.java class to do all out operations like creating WSDL file, creating stub file etc.<\/li>\n<li>Generate WSDL file using Java2WSDL: Axis has a tool called Java2WSDL, which generates a WSDL file for a web service using a Java class. Java2WSDL file takes the following arguments.\n<ol>\n<li>o \u2013 name for WSDL file -&gt; calculator.wsdl<\/li>\n<li>n \u2013 target namespace -&gt; mx:com.itechgenie.services.Calculator<\/li>\n<li>l \u2013 url of web service -&gt; <em><a href=\"http:\/\/%3Chost:port%3e\/%3cProject-Name%3e\/services\/calculator\">http:\/\/&lt;host:port&gt;\/&lt;Project-Name&gt;\/services\/calculator<\/a><\/em><\/li>\n<\/ol>\n<p>Summing up the above arguments the following command line arguments is created.<\/p>\n<pre class=\"lang:java decode:true\" title=\"Java2WSDL Arguments\">String java2wsdlArgs[] = {\"-ocalculator.wsdl\", \"-nmx:com.itechgenie.services.Calculator\", \"-v\", \"-lhttp:\/\/localhost:8080\/axis\/services\/calculator\", \"com.itechgenie.services.Calculator\"} ;<\/pre>\n<p>Read this Article on how to run the command line java tools from Eclipse.<br \/>\nYou can run the Java2WSDL as follows in the TestRunner class. Naah, don\u2019t ask how, just put the following lines the main method and press CTRL + F11.<\/p>\n<pre class=\"lang:java decode:true\" title=\"Run Java2WSDL from Main method\">try {\n\tJava2WSDL.main(java2wsdlArgs) ;\n} catch (Exception e) {\n\te.printStackTrace() ;\n}<\/pre>\n<p>The Java2WSDL class has the <strong><em>System.exit(0);<\/em><\/strong> method called from inside. So lines after the Java2WSDL will not be executed. To get the other arguments supported you can just run <strong><em>Java2WSDL.main(new String[0]) ;<\/em><\/strong>. This will display all the arguments supported by Java2WSDL Utility and this works for other utilities also.<br \/>\nAfter running this Utility you will find the calculator.wsdl file created in the root folder of the Project.<\/li>\n<li>Generate Server side and Client side codes using WSDL2Java: WSDL2Java is another tools provided by the AXIS, which can generate server side and client side Java classes using a WSDL file. These classes are needed for deploying the web service and also for accessing the web service using a Java client. This tool expects the following argument which includes the WSDL file generated in the last step.\n<ol>\n<li>o \u2013 output folder -&gt; src<\/li>\n<li>p \u2013 package for generated classes -&gt; mx:com.itechgenie.services. generated<\/li>\n<li>s \u2013 generate server side classes as well<\/li>\n<li>*.wsdl \u2013 WSDL file of any web service<\/li>\n<\/ol>\n<p>Summing up the above arguments the following command line arguments is created.<\/p>\n<pre class=\"lang:java decode:true\" title=\"WSDL2Java Arguments\">String wsdl2javaArgs[] = {\"-osrc\", \"-pcom.itechgenie.generated.service\", \"-s\", \"calculator.wsdl\", \"-v\"} ;<\/pre>\n<p>Read this Article on how to run the command line java tools from Eclipse.<br \/>\nNow run the WSDL2Java utility as follows.<\/p>\n<pre class=\"lang:java decode:true\" title=\"Run WSDL2Java from Main method\">try {\n\tWSDL2Java.main(wsdl2javaArgs) ;\n} catch (Exception e) {\n\te.printStackTrace() ;\n}<\/pre>\n<p>Once the above command is run, Just refresh the project in eclipse, you will find the following files created inside the \u201c<em>com.itechgenie.generated.service<\/em>\u201d package.<\/p>\n<ol>\n<li>Calculator.java<\/li>\n<li>CalculatorService.java<\/li>\n<li>CalculatorServiceLocator.java<\/li>\n<li>CalculatorSoapBindingImpl.java<\/li>\n<li>CalculatorSoapBindingStub.java<\/li>\n<li>deploy.wsdd<\/li>\n<li>undeploy.wsdd<\/li>\n<\/ol>\n<p>The above files can be used in both Server and Clients side as Skeleton (CalculatorSoapBindingImpl.java) and the Stub (CalculatorSoapBindingStub.java) respectively.<\/li>\n<li>Binding the business logic with the Skeleton: Take the Skeleton file and you will find the exact methods that were available in our Business logic class (Calculator.java.).<br \/>\nCreate a instance of the Business class and invoke the appropriate method from the skeleton as follows (Find the lines highlighted in yellow.).<\/p>\n<pre class=\"lang:java mark:7,10,14,18,22 decode:true\" title=\"CalculatorSoapBindingImpl.java\">package com.itechgenie.generated.service;\n\nimport com.itechgenie.services.Calculator;\n\npublic class CalculatorSoapBindingImpl implements com.itechgenie.generated.service.Calculator{\n\n\tCalculator calculatorImpl = new Calculator() ;\n\n    public int add(int in0, int in1) throws java.rmi.RemoteException {\n    \treturn calculatorImpl.add(in0, in1) ;\n    }\n\n    public int subtract(int in0, int in1) throws java.rmi.RemoteException {\n    \treturn calculatorImpl.subtract(in0, in1) ;\n    }\n\n    public int divide(int in0, int in1) throws java.rmi.RemoteException {\n    \treturn calculatorImpl.divide(in0, in1) ;\n    }\n\n    public int multiply(int in0, int in1) throws java.rmi.RemoteException {\n    \treturn calculatorImpl.multiply(in0, in1) ;\n    }\n\n}<\/pre>\n<p>That\u2019s it; we are now done with the development part of the Web Service. All we have to do is to configure to make the service up and running.<\/li>\n<li>Last configurations to make our service available: Open the server-config.wsdd file inside the WEB-INF folder. You will find the following lines.\n<pre class=\"lang:xhtml decode:true\" title=\"Service tag place holder in server-config.wsdd\">  &lt;!--  Your Service from the deploy.wsdd file - Starts here --&gt;\n\n  &lt;!--  Your Service from the deploy.wsdd file - Ends here --&gt;<\/pre>\n<p>Keep the file aside and open the deploy.wsdd from the WSDL2Java generated files. Copy the <strong><em>&lt;service&gt; \u2026 &lt;\/service&gt;<\/em><\/strong> tag completely and paste in between the comments said above.<\/li>\n<li>Conclusion: You can follow the steps 6 to 11 and create as many services as you want and paste them in the server-config.wsdd.<br \/>\nWith this the configurations for the Web Service is over. Export the Project as a War and deploy it in Web Server and point to the URL <em><a href=\"http:\/\/%3Chost:port%3e\/%3cProject-Name%3e\/services\/calculator\">http:\/\/&lt;host:port&gt;\/&lt;Project-Name&gt;\/services<\/a><\/em><\/li>\n<p>This URL should display all the services generated from steps 6 to 11 with the links the WSDL files for the above.<\/li>\n<p> <a href=\"http:\/\/itechgenie.com\/myblog\/wp-content\/uploads\/2012\/10\/MyWebService.zip\" title=\"MyWebService.zip\" target=\"_blank\">Click here to download the sample project.<\/a>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>With all the basic configurations done as specified in the last Article we continue to develop the Business logic. Create a class named Calculator.java, place four public methods add, subtract, multiply and delete and place the appropriate logics in it. package com.itechgenie.services.impl; public class Calculator { public int add(int a, int b) { return a+b &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/itechgenie.com\/myblog\/2012\/10\/create-web-services-using-axis-java2wsdl-wsdl2java-and-eclipse-for-all-servers-manually-part-2\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Create Web Services using Axis Java2WSDL, WSDL2Java and Eclipse for all Servers manually &#8211; Part 2&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5,7,9,12,13],"tags":[24,33,41,53,84],"class_list":["post-313","post","type-post","status-publish","format-standard","hentry","category-how-to","category-open-source","category-snippets","category-utility","category-web-services","tag-develope-website","tag-guide","tag-java","tag-tips-tricks","tag-web-services"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2HHtz-53","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/itechgenie.com\/myblog\/wp-json\/wp\/v2\/posts\/313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itechgenie.com\/myblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itechgenie.com\/myblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itechgenie.com\/myblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itechgenie.com\/myblog\/wp-json\/wp\/v2\/comments?post=313"}],"version-history":[{"count":0,"href":"https:\/\/itechgenie.com\/myblog\/wp-json\/wp\/v2\/posts\/313\/revisions"}],"wp:attachment":[{"href":"https:\/\/itechgenie.com\/myblog\/wp-json\/wp\/v2\/media?parent=313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itechgenie.com\/myblog\/wp-json\/wp\/v2\/categories?post=313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itechgenie.com\/myblog\/wp-json\/wp\/v2\/tags?post=313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}