webservice--the development of Webservice__web by contract priority

Source: Internet
Author: User
Tags soap wrapper wsdl

I. Basic ConceptsThere are two ways to develop webservice, such as code first and contract priority, this example introduces the webservice of contract priority.
There are three ways to write WSDL: The wrapper approach based on document, the bare approach based on document, and RPC based approach. This example describes the Wraper method, which is also the default and recommended method. Wrapper has the meaning of wrapping up all objects through element encapsulation.

II. Preparation of steps
① Service Side
1. Write 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/hello/" xmlns:wsdl= "http://schemas.xmlsoap.org/wsdl/"
Xmlns:xsd= "Http://www.w3.org/2001/XMLSchema"
Name= "HelloService"
Targetnamespace= "http://www.example.org/hello/" >
<wsdl:types>
<xsd:schema targetnamespace= "http://www.example.org/hello/" >
<xsd:element name= "Add" type= "Tns:add" ></xsd:element>
<xsd:element name= "Addresponse" type= "Tns:addresponse" ></xsd:element>
<xsd:element name= "Licenceinfo" type= "Tns:licenceinfo" ></xsd:element>
<xsd:complextype name= "Add" >
<xsd:sequence>
<xsd:element name= "A" type= "Xsd:int" ></xsd:element>
<xsd:element name= "B" type= "Xsd:int" ></xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complextype name= "Addresponse" >
<xsd:sequence>
<xsd:element name= "Addresponse" type= "Xsd:int" ></xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complextype name= "Licenceinfo" >
<xsd:sequence>
<xsd:element name= "Licenceinfo" type= "xsd:string" ></xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types>

<wsdl:message name= "Add" >
<wsdl:part name= "Add" element= "Tns:add" ></wsdl:part>
</wsdl:message>
<wsdl:message name= "Addresponse" >
<wsdl:part name= "Addresponse" element= "Tns:addresponse" ></wsdl:part>
</wsdl:message>
<wsdl:message name= "Licenceinfo" >
<wsdl:part name= "Licenceinfo" element= "Tns:licenceinfo" ></wsdl:part>
</wsdl:message>

<wsdl:porttype name= "Ihelloservice" >
<wsdl:operation name= "Add" >
<wsdl:input message= "Tns:add" ></wsdl:input>
<wsdl:output message= "Tns:addresponse" ></wsdl:output>
</wsdl:operation>
</wsdl:portType>

<wsdl:binding name= "Helloservicesoap" type= "Tns:ihelloservice" >
<soap:binding style= "Document" transport= "Http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name= "Add" >
<wsdl:input>
<soap:body use= "literal"/>
<soap:header use= "literal" part= "Licenceinfo" message= "Tns:licenceinfo" ></soap:header>
</wsdl:input>
<wsdl:output>
<soap:body use= "literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>

<wsdl:service name= "HelloService" >
<wsdl:port binding= "Tns:helloservicesoap" name= "Helloserviceport" >
<soap:address location= "Http://localhost:8080/hello"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

2, the generation of Java files through Wsimport, the generated Java class files copied to the project in general under the SRC new MATE-INF/WSDL two-tier folder, put the file in.

3, delete the generated Java files, only the generated interface to the. java file, delete the file does not need code.be careful not to copy the. class file in, because in the future if you want to deploy in Tomcat, the extra. class file will cause the program to complain.

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.1.1 in JDK 6
* Generated Source version:2.1
*
*/
@WebService (name = "Hello", targetnamespace = "http://www.example.org/hello/")
Public interface Ihelloservice {

@WebMethod
@WebResult (name = "Addresponse", targetnamespace = "")
@RequestWrapper (localname = "Add", targetnamespace = "http://www.example.org/hello/", ClassName = " Org.example.hello.Add ")
@ResponseWrapper (localname = "Addresponse", targetnamespace = "http://www.example.org/hello/", ClassName = " Org.example.hello.AddResponse ")
public int Add (
@WebParam (name = "a", targetnamespace = "")
int A,
@WebParam (name = "B", targetnamespace = "")
int B,
@WebParam (name= "Licenceinfo", Header=true) String licenceinfo);
}

4, the preparation of Helloserviceimpl class
@WebService (endpointinterface = "Org.example.hello.Hello",
ServiceName = "HelloService", wsdllocation = "meta-inf/wsdl/hello.wsdl",
targetnamespace = "http://www.example.org/hello/", Portname= "Helloserviceport")
public class Helloserviceimpl implements Ihelloservice {

public int Add (int a, int b, String licenceinfo) {
System.out.println (A+B);
System.out.println ("Licenceinfo:" + licenceinfo);
return a + B;
}

}

5. Release service
Import Javax.xml.ws.Endpoint;
Import Org.example.hello.HelloServiceImpl;

public class Servicetest {
public static void Main (string[] args) {
Endpoint.publish ("Http://localhost:8080/mywebservice", New Helloserviceimpl ());

}

}

② Writing Client
Add the Java class generated by Wsimort to the client.

1, the normal way to access (unable to deliver header information, of course, you can also write handler add header information)
Import Java.net.URL;
Import Javax.xml.namespace.QName;
Import Javax.xml.ws.Service;
Import Org.example.hello.Hello;

public class Client01 {
public static void Main (string[] args) throws exception{
URL url = new URL ("http://localhost:8080/mywebservice?wsdl");
String namespace = "http://www.example.org/hello/";
QName sname = new QName (namespace, "HelloService");
Service service = service.create (URL, sname);
Ihelloservice Hello = Service.getport (Ihelloservice.class);
int result = Hello.add (1, 100);
SYSTEM.OUT.PRINTLN (result);

Or call the generated method directly (this is another example that can be modeled in writing)
Myserviceimplservice mis = new Myserviceimplservice ();
Imyservice ms = Mis.getmyserviceimplport ();
System.out.println (Ms.add (29, 3));
}
}

2, assembled SoapMessage way access (with header information)
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.SOAPHeader;
Import Javax.xml.soap.SOAPMessage;
Import Javax.xml.ws.Dispatch;
Import Javax.xml.ws.Service;

public class Client02 {

/**
* Request with Header
*/
public static void Main (string[] args) throws exception{
URL url = new URL ("http://localhost:8080/mywebservice?wsdl");
String namespace = "http://www.example.org/hello/";
QName sname = new QName (namespace, "HelloService");

Service service = service.create (URL, sname);
QName protname = new QName (namespace, "Helloserviceport");
dispatch<soapmessage> Dispatch = Service.createdispatch (Protname,soapmessage.class, Service.Mode.MESSAGE);

SoapMessage msg = Messagefactory.newinstance (). CreateMessage ();
SoapEnvelope env = Msg.getsoappart (). Getenvelope ();
Soapbody BODY = Env.getbody ();
SoapHeader Header = Env.getheader ();
if (header = = null) Header = Env.addheader ();

Be sure to add the NS prefix
QName addname = new QName (namespace, "Add", "Myprefix");
Add body Information
Soapbodyelement Bodyele = body.addbodyelement (addname);
Bodyele.addchildelement ("a"). SetValue ("1");
Bodyele.addchildelement ("B"). SetValue ("2");

Add header information
QName headername = new QName (namespace, "Licenceinfo");
Set the value of the header information
Header.addheaderelement (Headername). Settextcontent ("admin");
Print SOAP messages before sending
Msg.writeto (System.out);

System.out.println ("----------------relult---------------");
SoapMessage resultmsg = Dispatch.invoke (msg);
Print the returned SOAP message
Resultmsg.writeto (System.out);
}

}

Original post address: http://blog.csdn.net/is_zhoufeng/article/details/8365723

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.