WebService working principle and example

Source: Internet
Author: User
Tags soap wsdl

Web Service working principle and example one, Web service basic concept Web service also called XML Web service WebService is a You can receive requests that are delivered from other systems on the Internet or intranet, and are lightweight and stand-alone communication technologies. is: Software services provided through SOAP on the web, described using WSDL files, and registered through UDDI.

XML: (extensible Markup Language) Extensible Markup Language. Temporary data processing for short-term, web-oriented network, is the basis of soap.

Soap: Simple Object access Protocol protocol. is the communication protocol for XML WEB Service. When a user finds your WSDL description document through UDDI, he can invoke one or more of the actions in the Web service that you set up by soap. SOAP is a specification for calling methods in the form of XML documents that can support different underlying interfaces, such as HTTP (S) or SMTP.

WSDL: (Web Services Description Language) A WSDL file is an XML document that describes a set of SOAP messages and how to exchange them. In most cases, it is automatically generated and used by the software.

UDDI (Universal Description, Discovery, and integration) is a new project primarily for Web service providers and users. Before a user can invoke a Web service, it is important to determine which business methods are included in the service, to find the called interface definition, and to prepare the software on the server side, and UDDI is a mechanism to guide the system through the description document to find the appropriate service. UDDI uses the SOAP message mechanism (standard xml/http) to publish, edit, browse, and find registration information. It uses XML format to encapsulate various types of data and send it to the registry or to the registry to return the required data. Second, invoke the principle of implementing a complete Web service consists of the following steps:

The Web service provider designs the implementation Web service and publishes the correct Web service through the Web Service Broker and registers with the UDDI registry; (publish)

A Web service requester requests a specific service from a Web Service Broker, who queries the UDDI registry on request, and finds the service that satisfies the request for the requestor;

The Web service mediator returns to the Web service requester the Web Service description information that satisfies the condition, which is written in WSDL and can be read by various machines that support Web services;

Generate the corresponding SOAP message from the description information returned by the Web service mediator and send it to the Web service provider for the invocation of the Web Service; (binding)

The Web service provider executes the corresponding Web service by SOAP message and returns the result of the service to the Web service requester. Binding


Three, call mode: 1. The simple and flexible method of dynamically calling WebService using Get/post/soap mode (C #)

There are 3 ways to call WebService

1). HttpGet
2). HttpPost
3). HttpSoap

The advantage of soap is that it can pass structured data, while the first two do not.
BTW, Soap eventually uses HTTP to send XM

WebService instance one, using CXF to develop a Web service server: Each Web Service component requires 2 parts: interface and Implementation class: Step 1, prepare to develop the required jar package "apache-cxf-2.5.9 download" 2, the development of a WebService business interface, methods using @webservice decoration. [Java]View PlainCopy
    1. Package com.ywx;
    2. Import Javax.jws.WebService;
    3. @WebService
    4. Public interface HelloWorld {
    5. String Sayhi (string name);
    6. }

3, write a implementation of this method of the class, the method also needs to use the @webservice decoration, and specify the parameters within it, the following specifies the desired implementation of the interface, and specify the service name. [Java]View PlainCopy
  1. Package Com.ywx.impl;
  2. Import Java.util.Date;
  3. Import Javax.jws.WebService;
  4. Import Com.ywx.HelloWorld;
  5. @WebService (endpointinterface="Com.ywx.HelloWorld", servicename="Helloworldws")// Specifies the interface that webservice implements and the name of the service
  6. Public class Hellowworlds implements helloworld{
  7. @Override
  8. Public string Sayhi (string name) {
  9. return name+"Hello!  Now the time is: "+new Date ();
  10. }
  11. }


4. Function exposing Web service, running function exposes Web service: [Java]View PlainCopy
  1. Package Com.ywx.lee;
  2. Import Javax.xml.ws.Endpoint;
  3. Import Com.ywx.HelloWorld;
  4. Import Com.ywx.impl.HellowWorlds;
  5. Public class ServiceMain {
  6. public static void Main (String args[]) {
  7. HelloWorld HW = new Hellowworlds ();
  8. //Call Endpoint's Publish method to publish the Web Service
  9. Endpoint.publish ("192.168.1.7/vashon", HW);
  10. System.out.println ("Web service exposed successfully!")  ");
  11. }
  12. }

Then run the browser, enter: http://192.168.1.7/vashon?wsdl to view the results, if the successful generation of the following WSDL document indicates a successful Web service exposure.    Ii. using CXF to develop a Web service client:  step:  1, create a new client project 2, call the Wsdl2java tool provided by CXF, or use the Eclipse/myeclipse new web Service generation client code (second way here):                                               input WSDL links:         &NB Sp                          ,         &NB SP, click next:                              / nbsp                                  &NBS P                          ,         &NB SP; Choose where to generate the client code:         &NBSP                          ,         &NB Sp     Click Finish to generate the client code as follows:                      & nbsp                          ,         &NB Sp                    3, write test class test on client:  [Java]View PlainCopy
  1. Package com.ywx.test;
  2. Import java.rmi.RemoteException;
  3. Import Com.ywx.HelloWorldProxy;
  4. Public class Testservice {
  5. public static void Main (String args[]) {
  6. Helloworldproxy h = new Helloworldproxy ();
  7. try {
  8. String s = h.sayhi ("Yangwenxue");
  9. System.out.println ("Tune WebService:" +s);
  10. } catch (RemoteException e) {
  11. E.printstacktrace ();
  12. }
  13. }
  14. }

Running the result (passing in a parameter, the string returned by the call to the WEB service results in the following): The format of its invocation is already defined by the server, see the code posted above or the following description: Web Service server and client engineering results are as follows:

WebService working principle and example

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.