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

WebService principle

高洛峰
Release: 2016-12-15 15:23:49
Original
1274 people have browsed it

Basic principle:
From a macro perspective, it is a web service based on the protocol (SOAP protocol). From a micro level, it is an application program, exposed to the outside world. External programs can call its API through the web. We previously wrote If you want to call a method of a dao or an mgr, describe it in Java language. You usually need to pass an instance of the class or class, and then call the method of the class. For example:
Class UserMgr{
public void checkUser();

}, if you want to call it, you can do this in your Action (assuming Struts is used as the client program)
UserMgr um = new UserMgr() ;
um.checkUser();//Note that a call is made here.
Now we only need to expose this UserMgr class to the outside world (web Service) and let its client program (caller/visitor-can be any program, as long as it can support WebService), for example, in c# The program needs to call an API (method) of a java application, here is an Add method.

package com.zhuweisky.xfireDemo; public class MathService { public int Add(int a ,int b) { return a+b ; } }
Copy after login
//C# string url = "http://localhost:8080/XFireZhuweiTest/services/MathService" ; object[] args ={1,2} ; object result = ESFramework.WebService.WebServiceHelper.InvokeWebService(url ,"Add" ,args) ; MessageBox.Show(result.ToString());
Copy after login

People who love thinking will definitely ask, the cross-platform call (WebService) definitely requires a protocol. The protocol here is the platform-independent RPC-----Remote Procedure Call Protocol---- -It can be truly interoperable. RPC consists of three parts.
1.1 XML and XSD
Smart people will definitely think of XML (Extensible Markup Language). It is a truly cross-platform data format - platform independent and vendor independent. XML solves the problem of data representation, but it still has There is a lack of a standard set of data types, let alone how to extend this set of data types. For example, what exactly does an integer represent? 16-bit, 32-bit, 64-bit, these are also very important for cross-platform. The XML Scheme (XSD) developed by W3C is a set of standards specifically designed to solve this problem. It defines a set of standard data types-- -This must be supported by any manufacturer. WebService uses XSD as the data type system. When you use a certain type of language (.Net or Java) to build a WebService, in order to comply with the WebService standard, all your data Types must be converted to XSD types. Generally speaking, the tool you use will help you complete this standard conversion work. Of course you can also define it yourself.
1.2 SOAP
After you build a WebService, the client program needs to call it. Simple Object Access Protocol (SOAP) provides standard RPC methods to call your WebService. In fact, SOAP is a bit of a misnomer here. It is not necessarily an object. You can write a function in C as a WebService, and it can still be called through SOAP. The SOAP specification defines the format of the SOAP message and how to use it through the HTTP protocol. to use SOAP. SOAP is also based on XML and XSD. XML is the data encoding format of SOAP. Let me guess the underlying implementation principle of SOAP. Taking java as an example, start a Servlet program. This Servlet accepts SOAP objects on the network (Http protocol) ------- It is possible to assume that it is not a Servlet of WebService. What is accepted is a normal HttpSerletRequest object. This SOAP object contains standard XML-based data. Then the first thing the Servlet needs to do is to parse the object, obtain enough information and then call the corresponding method.
1.3 WSDL
How would you introduce to others what functions your web service has? And what about the parameters when each function is called? You may write a document or verbally tell the people who need to call your WebService. A serious problem with these informal methods is that their tools (such as Eclipse or Visio Studio) cannot provide any help because your tools do not know how to do it. To understand your WebService, one solution is to use a document that the machine can understand. A description language (WSDL) of WebService is such an XML-based language. Use WebService and its functions and function parameters and return values, because this is Based on xml, so WSDL can be read by machines and humans,

4. Purpose:
System integration, data exchange between systems, distributed computing, and interoperability of different platforms. To put it bluntly, you can use C# programs to call Java programs on .Net (the requirement is that your program is based on WebService)

5. Practical part
Take java as an example. First of all, it is specially stated that the frameworks that currently support WebService are There are many (these frameworks do some basic work, making it very convenient for you to write a WebService, which is the same as the difference between using Hibernate and JDBC). Here we take the xFire framework integrated in MyEclipse as an example. At present, the development momentum of this framework is very, The support for Spring is also very sufficient, and there are many similar frameworks that support WebService, such as axis.
First use MyEclipse to create a WebService project, and then write the interface and implementation class,

public interface IService { public String testMethod(String testStr); }
Copy after login

Implementation class:

public class ECIWebservice implements IService { public void putMessage(MessageInfo message, String authInfo) throws UniediException { // TODO Auto-generated method stub } public void putMessages(MessageInfo[] messages, String authInfo) throws UniediException { // TODO Auto-generated method stub } public MessageInfo getMessage(String appId, String orgCode, String authInfo) throws UniediException { // TODO Auto-generated method stub return null; } public MessageInfo[] getMessages(String appId, String orgCode, String authInfo, int count) throws UniediException { // TODO Auto-generated method stub return null; } public String testMethod(String testStr) { System.out.println("&&&&&&&&&&&&&&&&&Hello: "+testStr+"******************"); return "The para is "+testStr; } }
Copy after login

建立一个WebService的工程,其实没有什么什么特别,仅仅是比普通的Tomcat工程多WebService.xml文件---这个时候的此文件啥内容都没有。

然后再建立一个WebService,这个时候会提示


默认会把当前所有的webService工程都列出来。选择其中一个,进入一下步:选择你的发布名字,以及要把那个API开放出去(接口和实现类,默认会把所有public的方法都开放出去)

点击完成。其实做这一步骤仅仅是为了修改webservice.xml文件。

请看修改后的文件内容:

   testwebservice com.webservice.test.IService  com.webservice.test.ECIWebservice   literal application 
Copy after login

至此,一个WebService已经建立完成,发布到web容器上(Tomcat和jboss都是可以的)你可以有三种方式来测试。

第一种方式是:

利用MyEclipse自带的Web service explorer。

点击go以后。

点击bindings的link以后,就能看到binding了的所有方法,这些方法都是可以在这里调用的。


我们以一个简单的方法testMethod()作为测试使用。

看看执行后得效果:

第二种方法就是通过,使用浏览器的:

直接在浏览器的地址栏里输入:

http://localhost:8080/mywebservice/services/testwebservice?wsdl


应该可以看到一个XML文件(此文件就是描述文件WSDL)的内容,不过这里只摘写部分内容:

 -  -      -  -  -      。。。。。    。。。。。     。。。。  -    
Copy after login

能看到这部分内容也是说明你的WebService已经好了。


第三种方法就是编写一个客户端程序去测试,特别说明的是,这个客户端可以人适合程序,asp,VB.net, C#, 等

我们是使用了一个java的Application

public static void main(String[] args){ IService is =null; org.codehaus.xfire.service.Service srvcModel = new ObjectServiceFactory().create(IService.class); XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire()); String helloWorldURL = "http://localhost:8080/mywebservice/services/testwebservice"; try { is = (IService)factory.create(srvcModel, helloWorldURL); is.testMethod("zhangsan"); }catch(Exception ex){ } }
Copy after login

程序顺利执行,其结果就在tomcat的控制台打印出来了我们想看到的内容了。这里需要特别说明的是,假如不用XFire框架,可是可以编写你自己的WebService的客户端程序的。或者使用了其他的框架(这里以Axis为例):

String endpoint = " http://localhost:8080/mywebservice/services/testwebservice"; Service service = new Service(); Call call = (Call)service.createCall(); call.setTargetEndpointAddress(endpoint); call.setOperationName(new QName("urn: testwebservice ", "testMethod")); String resultStr = ((String)call.invoke(new Object[]{theIntArray})); System.out.println("result_int: " + resultStr);
Copy after login



更多WebService原理相关文章请关注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
    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!