Home > php教程 > PHP开发 > body text

Webservice working principle and examples

高洛峰
Release: 2016-12-15 14:58:51
Original
1464 people have browsed it

I Web Service Working Principles










Webservice working principle and examples













Web Service, also known as XML Web Service WebService, is a request that can receive from other systems from Internet or Intranet. Lightweight independent independent Communication Technology. Yes: A software service delivered over the Web via SOAP, described using a WSDL file, and registered via UDDI.

XML: (Extensible Markup Language) Extensible Markup Language. For short-term temporary data processing and the World Wide Web, it is the foundation of Soap.

Soap: (Simple Object Access Protocol) Simple Object Access Protocol. It is the communication protocol of XML Web Service. When the user finds your WSDL description document through UDDI, he can call one or more operations in the Web service you created through SOAP. SOAP is a specification for calling methods in the form of XML documents. It 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 automatically generated and used by software.

UDDI (Universal Description, Discovery, and Integration) is a new project mainly aimed at Web service providers and users. Before users can call a Web service, they must determine which business methods are included in the service, find the called interface definition, and compile software on the server side. UDDI is a mechanism that guides the system to find the corresponding service based on the description document. UDDI uses SOAP messaging mechanism (standard XML/HTTP) to publish, edit, browse and find registration information. It uses XML format to encapsulate various types of data, and sends it to the registration center or the registration center returns the required data.


2. Calling Principle




Implementing a complete Web service includes the following steps:

◆ The Web service provider designs and implements the Web service, and publishes the correctly debugged Web service through the Web service intermediary, and Register in the UDDI registration center; (Release)

◆ The Web service requester requests a specific service from the Web service intermediary, and the intermediary queries the UDDI registration center according to the request to find services that satisfy the request for the requester; (Discovery)

◆ The Web service intermediary returns to the Web service requester the Web service description information that meets the conditions. The description information is written in WSDL and can be read by various machines that support Web services; (Discovery)

◆ Use the information returned from the Web service intermediary The description information generates the corresponding SOAP message and sends it to the Web service provider to implement the invocation of the Web service; (Binding)

◆ The Web service provider executes the corresponding Web service according to the SOAP message and returns the service result to the Web service Requester. (Binding)

3. Calling method: Webservice working principle and examples

1. A simple and flexible method to dynamically call WebService using GET/POST/SOAP under Net (C#)

There are 3 ways to call webservice

1) . httpget

2). httppost

3). The advantage of httpsoap

soap is that it can pass structured data, but the first two cannot.

btw, soap ultimately uses HTTP to transmit Steps: Webservice working principle and examples

1. Prepare the jar package needed for development [apache-cxf-2.5.9 download] 🎜🎜🎜🎜🎜2. Develop a webservice business interface, and use @WebService to modify the method. 🎜
package com.ywx;  
  
import javax.jws.WebService;  
  
@WebService  
public interface HelloWorld {  
    String sayHi(String name);  
}
Copy after login
🎜3. Write an implementation class for this method. The method also needs to be modified with @WebService and specify the parameters. The following specifies the interface to be implemented and the service name. 🎜
package com.ywx.impl;  
  
import java.util.Date;  
  
import javax.jws.WebService;  
  
import com.ywx.HelloWorld;  
@WebService(endpointInterface="com.ywx.HelloWorld",serviceName="HelloWorldWs")//指定webservice所实现的接口以及服务名称  
public class HellowWorlds implements HelloWorld{  
  
    @Override  
    public String sayHi(String name) {  
        return name+"您好!现在时间是:"+new Date();  
    }  
  
}
Copy after login
🎜4. Expose the function of Web Service. Run the function to expose Web Service: 🎜
package com.ywx.lee;  
  
import javax.xml.ws.Endpoint;  
  
import com.ywx.HelloWorld;  
import com.ywx.impl.HellowWorlds;  
  
public class ServiceMain {  
    public static void main(String args[]){  
        HelloWorld hw = new HellowWorlds();  
        //调用Endpoint的publish方法发布Web Service  
        Endpoint.publish("192.168.1.7/vashon", hw);  
        System.out.println("Web Service暴露成功!");  
    }  
}
Copy after login
🎜 Then run the browser and enter: http://192.168.1.7/vashon?wsdl to view the results. If the following wsdl document is successfully generated, it means Web Service exposed successfully. 🎜🎜🎜🎜

二、使用CXF开发Web Service客户端:

步骤:

1、新建一个客户端工程

2、调用CXF提供的wsdl2java工具或使用eclipse/myeclipse的new Web Service生成客户端代码(这里使用第二种方式):

Webservice working principle and examples

输入wsdl链接:

Webservice working principle and examples

点击next:

Webservice working principle and examples

选择生成客户端代码的位置:

Webservice working principle and examples

点击finish,生成客户端代码如下:

Webservice working principle and examples

3、在客户端写测试类测试:

package com.ywx.test;  
  
import java.rmi.RemoteException;  
  
import com.ywx.HelloWorldProxy;  
  
  
public class TestService {  
    public static void main(String args[]){  
        HelloWorldProxy h = new HelloWorldProxy();  
        try {  
            String s = h.sayHi("yangwenxue");  
            System.out.println("调webservice:"+s);  
        } catch (RemoteException e) {  
            e.printStackTrace();  
        }  
    }  
}
Copy after login

运行结果(传入一个参数,调用Web Service返回的字符串结果如下):

Webservice working principle and examples

其调用生成的格式已经有服务端定义好了,看上面贴出来的代码或者下面的截图说明:

Webservice working principle and examples

Web Service服务端和客户端工程结果截图如下:

Webservice working principle and examples



更多Webservice working principle and examples相关文章请关注PHP中文网!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!