Example of creating and using WSDL WebService
Last Update:2016-07-15
Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size:14px"><strong>I. Creation of the WSDL webservice:</strong></span><br><span style="font-size:12px">1. Create "Web Service Project":</span><br><span style="color:#ffffff; font-size:12px"></span><br><br><span style="font-size:12px">WebServices Framework to choose Jax-ws:</span><br><span style="font-size:12px"></span><br><br><span style="font-size:12px">2. Write a simple test case:</span><br></p></p><pre name="code" class="java" style="font-size:12px; "><pre name="code" class="java" style="font-size:12px; ">Package Com.webservice;public class Webservice{public string printdata (string printername) {string strret = ' Welcome to us E WebService, "+ printername; System.out.println ("Print from WebService:" + strret); return strret;}}</pre></pre><br><span style="font-size:12px"><span style="font-size:12px">3. Publish the web Service:</span></span><br><span style="font-size:12px"><span style="font-size:12px">Click New Web Service on the toolbar:</span></span><br><span style="font-size:12px"><span style="font-size:12px"></span></span><br><br><span style="font-size:12px"><span style="font-size:12px">Strategy Select a second (Create Web service from Java class):</span></span><br><span style="font-size:12px"><span style="font-size:12px"></span></span><br><br><span style="font-size:12px"><span style="font-size:12px">tick "Generate WSDL in project":</span></span><br><span style="font-size:12px"><span style="font-size:12px"></span></span><br><br><span style="font-size:12px"><span style="font-size:12px">after clicking "Finish", the system will generate two files under Web-inf/wsdl:</span></span><br><span style="font-size:12px"><span style="font-size:12px"></span></span><br><br><span style="font-size:12px"><span style="font-size:12px">webserviceservice.wsdl: This file is used to describe the Web service content<br></span></span><pre name="code" class="html" style="font-size:12px; "><?xml version= "1.0" encoding= "UTF-8"?><!--Generated by Jax-ws RI at Http://jax-ws.dev.java.net. Ri ' s version is Jax-ws RI 2.1.3-hudson-390-. --><definitions xmlns= "http://schemas.xmlsoap.org/wsdl/" xmlns:soap= "http://schemas.xmlsoap.org/wsdl/soap/ "xmlns:tns=" http://webservice.com/"xmlns:wsu=" http://docs.oasis-open.org/wss/2004/01/ Oasis-200401-wss-wssecurity-utility-1.0.xsd "xmlns:xsd=" Http://www.w3.org/2001/XMLSchema "name=" Webserviceservice "targetnamespace=" http://webservice.com/"> <types> <xsd:schema> <xsd:import N amespace= "http://webservice.com/" schemalocation= "webserviceservice_schema1.xsd"/> </xsd:schema> </ types> <message name= "printdata" > <part element= "tns:printdata" name= "parameters"/> </message> <message name= "printdataresponse" > <part element= "tns:printdataresponse" name= "parameters"/> </ message> <porttype name= "webservicedelegate" > <operation name= "PRIntdata "> <input message=" tns:printdata "/> <output message=" tns:printdataresponse "/> </oper ation> </portType> <binding name= "webserviceportbinding" type= "tns:webservicedelegate" > <soap: Binding style= "document" transport= "http://schemas.xmlsoap.org/soap/http"/> <operation name= "printdata" > <soap:operation soapaction= ""/> <input> <soap:body use= "literal"/> </input> <output> <soap:body use= "literal"/> </output> </operation> </binding> <s Ervice name= "webserviceservice" > <port binding= "tns:webserviceportbinding" name= "webserviceport" > <soa P:address location= "http://localhost:8080/WebService/WebServicePort"/> </port> </service></ Definitions></pre><br><span style="font-size:12px"><span style="font-size:12px">webserviceservice_schema1.xsd: commands and their parameters used to describe a Web service<br></span></span><span style="font-size:12px"><span style="font-size:12px">for Example: The WebService in sample is "printdata", has a string type parameter "arg0", returns a value of a string type. </span></span><br><pre name="code" class="html" style="font-size:12px; "><pre name="code" class="html" style="font-size:12px; "><?xml version= "1.0" encoding= "UTF-8"? ><xs:schema xmlns:xs= "http://www.w3.org/2001/XMLSchema" xmlns:tns= " http://webservice.com/"targetnamespace=" http://webservice.com/"version=" 1.0 "> <xs:element name=" Printdata "type=" tns:printdata "/> <xs:element name=" printdataresponse "type=" tns:printdataresponse "/ > <xs:complextype name= "printdata" > <xs:sequence> <xs:element minoccurs= "0" name= " arg0 "type=" xs:string "/> </xs:sequence> </xs:complexType> <xs:complextype name=" Printdataresponse "> <xs:sequence> <xs:element minoccurs=" 0 "name=" return "type=" xs:string "/ > </xs:sequence> </xs:complexType></xs:schema></pre></pre><br><span style="font-size:12px"><span style="font-size:12px">Deploy the WebService project to Tomcat.<br>( <span style="font-size:12px">deployment method slightly</span> )</span></span><br><br><strong><strong><span style="font-size:14px">two. Invocation of WSDL webservice:</span><br>Method 1: Create a Web Service client to Invoke:<br></strong></strong>1. Create "Java Project":<br><br><br>2.<span style="font-size:12px"><span style="font-size:12px">Click the new Web Service Client on the toolbar:</span></span><br><br><br><br><br>3. Select "WSDL URL":<br><br><br>4. After clicking "next" to complete the creation, the relevant files are automatically generated under Src/com/webservice. (except webservicetest.java, This is a call file created by Itself)<br><br><br>5. Create "webservicetest.java"<br><br><br>The code is as Follows:<br><pre name="code" class="java"><pre name="code" class="java">Package Com.webservice;public class Webservicetest{public static void main (string[] Args) {webserviceservice Wssprintdata = new Webserviceservice (); Webservicedelegate Wsdprintdata = Wssprintdata.getwebserviceport (); System.out.println (wsdprintdata.printdata ("sun"));}}</pre></pre><br>6. "webservicetest.java" Right Button →run As→java application<br>Output result:<br><pre name="code" class="plain"><pre name="code" class="plain">Welcome to use WebService, Sun</pre></pre><br><br><strong><strong>method 2: Call with Httpclient:<br></strong></strong><pre name="code" class="java">Package Com.httpclientforwsdl;import Java.io.bytearrayinputstream;import Java.io.inputstream;import Java.util.hashmap;import Java.util.iterator;import Java.util.map;import org.apache.commons.httpclient.HttpClient; Import Org.apache.commons.httpclient.methods.inputstreamrequestentity;import Org.apache.commons.httpclient.methods.postmethod;import org.apache.commons.httpclient.methods.RequestEntity; public class Webservicehttpclienttest{public synchronized static string Accessservice (String wsdl,string ns,string Method,map<string,string> params,string Result) throws exception{//splicing parameter String param = GetParam (pa rams); String Soapresponsedata = ""; Splicing soap StringBuffer soaprequestdata = new StringBuffer (""); Soaprequestdata.append ("<soap:envelope xmlns:soap=\" http://schemas.xmlsoap.org/soap/envelope/\ ">"); Soaprequestdata.append ("<soap:Body>"); Soaprequestdata.append ("<ns1:" +method+ "xmlns:ns1=\" "+ns+"\ ">"); Soaprequestdata.append (param); Soaprequestdata.append ("</ns1:" +method+ ">"); Soaprequestdata.append ("</soap:Body>" + "</soap:Envelope>"); Postmethod Postmethod = new Postmethod (wsdl); The SOAP request data is then added to the Postmethod byte[] b=null; InputStream is=null; Try {b = soaprequestdata.tostring (). getBytes ("utf-8"); is = new Bytearrayinputstream (b, 0, b.length); requestentity re = new inputstreamrequestentity (is, b.length, "text/xml; charset=utf-8"); Postmethod.setrequestentity (re); HttpClient HttpClient = new HttpClient (); int status = Httpclient.executemethod (postmethod); System.out.println ("status:" +status); If (status==200) {soapresponsedata = getmesage (postmethod.getresponsebodyasstring (), result); }} catch (Exception e) {e.printstacktrace (); } finally{if (is!=null) {is.close (); }} return soapresponsedata; public static String getparam (map<string,string> params) {string param = ""; If (params!=null) {iterator<string> it = params.keyset (). Iterator (); While (it.hasnext ()) {String str = it.next (); param+= "<" +str+ ">"; Param+=params.get (str); param+= "</" +str+ ">"; }} return param; The public static string Getmesage (string soapattachment,string result) {System.out.println ("message:" +s oapattachment); If (result==null) {return null; } if (soapattachment!=null && soapattachment.length () >0) {int begin = Soapattachment.index of (result); begin = Soapattachment.indexof (">", begin); int end = Soapattachment.indexof ("</" +result+ ">"); String str = soapattachment.substring (begin+1, end); str = Str.replaceall ("<", "<"); str = Str.replaceall (">", ">"); Return str; }else{return ""; }}/** * @param args */public static void main (string[] Args) {try { map<string,string> param = new hashmap<string,string> (); Param.put ("arg0", "sun"); String wsdl= "http://localhost:8080/WebService/WebServicePort?wsdl"; String ns = "http://webservice.com/"; String method= "printdata"; String Response =accessservice (wsdl,ns,method,param, "return"); System.out.println ("main:" +response); } catch (Exception E) {e.printstacktrace (); } } }</pre><br>Show Results:<br><pre name="code" class="plain"><pre name="code" class="plain">status:200 July 15, 2016 3:43:27 pm org.apache.commons.httpclient.HttpMethodBase getresponsebody warning: going to buffer Response body of large or unknown size. Using Getresponsebodyasstream instead is recommended.message:<?xml version= "1.0"? ><s:envelope xmlns:S= "http ://schemas.xmlsoap.org/soap/envelope/"><s:body><ns2:printdataresponse xmlns:ns2="/http Webservice.com/"><return>welcome to use webservice, sun</return></ns2:printdataresponse></ S:body></s:envelope>main:welcome to use WebService, Sun</pre></pre><br><strong><strong>the personal understanding of WSDL WebService and restful webservice:<br><br><strong>Examples of creating and using RestFul webservice:<br></strong></strong></strong><p><p><strong><strong>http://blog.csdn.net/sunroyi666/article/details/51918675<br><br></strong></strong></p></p><strong><strong><strong>Sample code for WSDL WebService and restful webservice:<br></strong>http://download.csdn.net/detail/sunroyi666/9577143<br><br></strong></strong><p><p></p></p><p><p>Example of creating and using WSDL WebService</p></p></span>