WebService (vi) generation of WebService service code instances based on contract-first WSDL files __web

Source: Internet
Author: User
Tags soap xmlns wsdl

The first is to introduce the whole process

Contract Priority Development process
1. Write the schema or WSDL file first
2. Generate a client code based on this file
3. Write the implementation class (Specify Wsdllocation on the implementation class)
4. Publishing services

Create WSDL
1. Creating a WSDL under Classpath meta-inf
2. Create a new WSDL and write the file
2.1 Writing Type
2.2 Writing a message
2.3 Writing porttype specifying interfaces and methods
2.4 Writing binding specifying encoding style
2.5 Write Service (name to be the same as namespace name)
3. Generate server-side code by name
Go inside the WSDL folder with wsimport-d D:\sid\workspace_webservice\wsimport-keep mywsdl.wsdl
Leave only imyservice one interface, and the others are deleted
4. Write an implementation class implementation interface

5. Specify the location of the WSDL on the implementation class
Specifies that the targetnamespace is the same as the interface (you can not specify, but you need to re-add the method's return callout, etc.)
@WebService (endpointinterface= "Org.example.mywsdl.IMyService",
Targetnamespace= "http://www.example.org/mywsdl/",
wsdllocation= "META-INF/WSDL/MYWSDL.WSDL")
6. Start the service
You can then modify the WSDL file directly at boot time by hot modification.
7. Execute server wsimport, generate client code
wsimport-d D:\sid\workspace_webservice\wsimport-keep-verbose http://localhost:8989/ms?wsdl

8. Join the Header
Modified in the WSDL file:
<!--defining header information--
<xsd:element name= "LicenseInfo" type= "xsd:string"/>
<!--defining header information--
<wsdl:message name= "LicenseInfo" >
<wsdl:part name= "LicenseInfo" element= "Tns:licenseinfo"/>
</wsdl:message>
<!--add header information to the method--
<soap:header use= "literal" part= "LicenseInfo" message= "Tns:licenseinfo"/>

Modify the Add method for Imyservice
public int Add (
@WebParam (name = "a", targetnamespace = "")
int A,
@WebParam (name = "B", targetnamespace = "")
int B,
@WebParam (name = "LicenseInfo", Header=true)
String licenseinfo);

The code is as follows:

MYWSDL.WSDL:

<?xml version= "1.0" encoding= "UTF-8" standalone= "no"?> <wsdl:definitions xmlns:soap= "/http schemas.xmlsoap.org/wsdl/soap/"xmlns:tns=" http://www.example.org/mywsdl/"xmlns:wsdl="/HTTP/ schemas.xmlsoap.org/wsdl/"xmlns:xsd=" Http://www.w3.org/2001/XMLSchema "name=" mywsdl "targetnamespace="/HTTP/ www.example.org/mywsdl/"> <wsdl:types> <xsd:schema targetnamespace=" http://www.example.org/mywsdl/" > <!--definition Method--<xsd:element name= "Add" type= "Tns:add"/> <xsd:element name= "Addresponse" Ty Pe= "Tns:addresponse"/> <xsd:element name= "divide" type= "Tns:divide"/> <xsd:element name= "DivideRespo NSE "type=" Tns:divideresponse "/> <!--define head information--<xsd:element name=" LicenseInfo "type=" xsd:string "/&
    	
  	Gt <!--definition Type-<xsd:complextype name= "Add" > <xsd:sequence> <xsd:element name= "a" Typ E= "Xsd:int"/> <xsd:element name= "B" type= "Xsd:int"/> &Lt;/xsd:sequence> </xsd:complexType> <xsd:complextype name= "Addresponse" > &LT;XSD:SEQUENCE&G
    			T <xsd:element name= "Addresult" type= "Xsd:int"/> </xsd:sequence> </xsd:complexType> <xs D:complextype name= "divide" > <xsd:sequence> <xsd:element name= "NUM1" type= "Xsd:int"/> & Lt;xsd:element name= "num2" type= "Xsd:int"/> </xsd:sequence> </xsd:complexType> <xsd:comp Lextype name= "Divideresponse" > <xsd:sequence> <xsd:element name= "Divideresult" type= "Xsd:int"/&gt
    		; </xsd:sequence> </xsd:complexType> </xsd:schema> </wsdl:types> <wsdl:message na Me= "Add" > <wsdl:part name= "Add" element= "Tns:add"/> </wsdl:message> <wsdl:message name= "Addrespon Se "> <wsdl:part name=" addresponse "element=" Tns:addresponse "/> </wsdl:message> <wsdl:message Name = "DiviDe "> <wsdl:part name=" divide "element=" Tns:divide "/> </wsdl:message> <wsdl:message name=" Dividere Sponse "> <wsdl:part name=" divideresponse "element=" Tns:divideresponse "/> </wsdl:message> <!--definition Header Info--<wsdl:message name= "LicenseInfo" > <wsdl:part name= "licenseinfo" element= "Tns:licenseinfo"/> & lt;/wsdl:message> <wsdl:porttype name= "Imyservice" > <wsdl:operation name= "Add" > <wsdl:inp UT message= "tns:add"/> <wsdl:output message= "Tns:addresponse"/> </wsdl:operation> <wsdl:op Eration name= "divide" > <wsdl:input message= "tns:divide"/> <wsdl:output message= "Tns:divideresponse "/> </wsdl:operation> </wsdl:portType> <wsdl:binding name=" Myservicesoap "type=" Tns:imyser Vice "> <soap:binding style=" Document "transport=" Http://schemas.xmlsoap.org/soap/http "/> <wsdl:operati
      On name= "Add" ><wsdl:input> <soap:body use= "literal"/> <!--Add header information--<soap:header use= "Lite Ral "part=" LicenseInfo "message=" Tns:licenseinfo "/> </wsdl:input> <wsdl:output> <soa
      P:body use= "literal"/> </wsdl:output> </wsdl:operation> <wsdl:operation name= "divide" >
        <wsdl:input> <soap:body use= "literal"/> </wsdl:input> <wsdl:output> <soap:body use= "literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> & Lt;wsdl:service name= "Myserviceimplservice" > <wsdl:port binding= "tns:myservicesoap" name= "MyServiceImplPort" > <soap:address location= "http://localhost:8989/ms"/> </wsdl:port> </wsdl:service> </w
 Sdl:definitions>

Leave only the Imyservice class and make changes:

Package org.example.mywsdl;
Import Javax.jws.WebMethod;
Import Javax.jws.WebParam;
Import Javax.jws.WebResult;
Import Javax.jws.WebService;
Import javax.xml.bind.annotation.XmlSeeAlso;
Import Javax.xml.ws.RequestWrapper;


Import Javax.xml.ws.ResponseWrapper;
 /** * This class is generated by the Jax-ws RI. * JAX-ws RI 2.2.4-B01 * Generated Source version:2.2 * */@WebService (name = "Imyservice", targetnamespace = "http:/
     /www.example.org/mywsdl/") public interface Imyservice {/** * * @param b * @param A * @return * Returns INT */@WebMethod @WebResult (name = "Addresult", targetnamespace = "") @RequestWrapper (LocalName = "Add", targetnamespace = "http://www.example.org/mywsdl/", ClassName = "Org.example.mywsdl.Add") @Respons Ewrapper (localname = "Addresponse", targetnamespace = "http://www.example.org/mywsdl/", ClassName = " Org.example.mywsdl.AddResponse ") public int Add (@WebParam (name =" a ", targetnamespace= "") int A, @WebParam (name = "B", targetnamespace = "") int B, @WebParam (name = "LicenseInfo

    ", Header=true) String licenseinfo); /** * * @param num2 * @param num1 * @return * returns int * * @WebMethod @WebRes Ult (name = "Divideresult", targetnamespace = "") @RequestWrapper (LocalName = "divide", targetnamespace = "http://www.e xample.org/mywsdl/", ClassName =" Org.example.mywsdl.Divide ") @ResponseWrapper (LocalName =" Divideresponse ", Targetna
        Mespace = "http://www.example.org/mywsdl/", ClassName = "org.example.mywsdl.DivideResponse") public int divide (
        @WebParam (name = "Num1", targetnamespace = "") int num1, @WebParam (name = "Num2", targetnamespace = "")

int num2);
 }

Publish a service after you create an implementation class

Package org.example.mywsdl;

Import Javax.xml.ws.Endpoint;

public class MyServer {public

	static void Main (string[] args) {
		//address is a endpoint.publish written in WSDL
		("HTTP// Localhost:8989/ms ", New Myserviceimpl ());
	}
}

After the client code is generated on the server side:

wsimport-d D:\sid\workspace_webservice\wsimport-keep-verbose http://localhost:8989/ms?wsdl

To add a test class:

Package org.example.test;
Import java.io.IOException;
Import java.net.MalformedURLException;

Import Java.net.URL;
Import Javax.xml.namespace.QName;
Import Javax.xml.soap.MessageFactory;
Import Javax.xml.soap.SOAPBody;
Import javax.xml.soap.SOAPBodyElement;
Import Javax.xml.soap.SOAPEnvelope;
Import javax.xml.soap.SOAPException;
Import Javax.xml.soap.SOAPHeader;
Import Javax.xml.soap.SOAPMessage;
Import Javax.xml.ws.Dispatch;


Import Javax.xml.ws.Service;
		public class Test {public static void main (string[] args) {String ns = "http://www.example.org/mywsdl/";
			try {QName name = new QName (ns, "Myserviceimplservice");
URL url = new URL ("http://localhost:8989/ms?wsdl");
Myserviceimplservice mis = new Myserviceimplservice (url,name);
Imyservice ms = Mis.getmyserviceimplport ();
			System.out.println (Ms.divide (19, 2));
			Use SOAP to deliver the message service service = Service.create (url,name);
			QName pname = new QName (ns, "Myserviceimplport"); dispatch<soapmessage> DisPatch = Service.createdispatch (PName, Soapmessage.class, Service.Mode.MESSAGE);
			SoapMessage msg = Messagefactory.newinstance (). CreateMessage ();
			SoapEnvelope enve = Msg.getsoappart (). Getenvelope ();
			SoapHeader Header = Enve.getheader ();
			Soapbody BODY = Enve.getbody ();
			if (header==null) {header = Enve.addheader ();
			} QName hname = new QName (ns, "LicenseInfo", "ns");
			Header.addheaderelement (Hname). SetValue ("Sssssssss");
			QName bname = new QName (ns, "Add", "ns");
			Soapbodyelement ele = body.addbodyelement (bname);
			Ele.addchildelement ("a"). SetValue ("12");
			
			Ele.addchildelement ("B"). SetValue ("12");
			Msg.writeto (System.out); System.out.println ("\ n invoking ...
			
			");
			SoapMessage rep = Dispatch.invoke (msg);
		Rep.writeto (System.out);
		} catch (Malformedurlexception e) {e.printstacktrace ();
		} catch (SoapException e) {e.printstacktrace ();
		} catch (IOException e) {e.printstacktrace ();
 }
	}

}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.