`
dxm1986
  • 浏览: 429852 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

WebService之XFire简单Demo

阅读更多

XFire是新一代的Java Web服务引擎,XFire使得在JavaEE应用中发布Web服务变得轻而易举。和其他Web服务引擎相比,XFire的配置非常简单,可以非常容易地和Spring集成,它使得Java开发人员终于可以获得和.Net开发人员一样的开发效率。 --摘自百度百科

 

 

服务器端怎么用XFire发布服务?

1 创建web工程

2 将XFire目录下的各种jar包拷贝到web工程的lib下面

3 修改 web.xml 文件,在其中增加如下 Servlet 定义内容

 

     <servlet-mapping>

	    <servlet-name>XFireServlet</servlet-name>
	    <url-pattern>/servlet/XFireServlet/*</url-pattern>
	  </servlet-mapping>
	
  	<servlet-mapping>
	    <servlet-name>XFireServlet</servlet-name>
	    <url-pattern>/services/*</url-pattern>
	</servlet-mapping>
 

4 创建service配制文件:src/META-INF/xfire/services.xml

<beans xmlns="http://xfire.codehaus.org/config/1.0">

	<service>
		<name>
			HelloWorldService
		</name>	
		<namespace>
			http://locahost:8888/xfiredemo/HelloWorldService
		</namespace>
		<serviceClass>
			com.dxm.cyou.xfire.pojo.HelloWorldService
		</serviceClass>
		<implementationClass>
			com.dxm.cyou.xfire.pojo.HelloWorldServiceImpl
		</implementationClass>
	</service>
</beans>

  5 很显然,步骤4之前我创立了一个接口,和一个接口实现类,名字从service.xml中可以看出

6 java文件如下:

com.dxm.cyou.xfire.pojo.HelloWorldService.java

package com.dxm.cyou.xfire.pojo;

public interface HelloWorldService {
	
	public String sayHelloWorld(String name,	Integer size);
	
	
	public Integer add(Integer a,	Integer b);

}

 

   package com.dxm.cyou.xfire.pojo;

public class HelloWorldServiceImpl implements HelloWorldService {

	public String sayHelloWorld(String name,	Integer size) {
		return "Hello World:" + name + "||size:" + size;
	}
	
	public Integer add(Integer a,	Integer b){
		return a + b;
	}
}

 

7 发布Web服务,在浏览器中访问URL:http://localhost:8888/xfiredemo/services/HelloWorldService?wsdl

 

 

得到如下结果:

<wsdl:definitions targetNamespace="http://locahost:8888/xfiredemo/HelloWorldService">

−
<wsdl:types>
−
<xsd:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://locahost:8888/xfiredemo/HelloWorldService">
−
<xsd:element name="sayHelloWorld">
−
<xsd:complexType>
−
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="xsd:string"/>
<xsd:element maxOccurs="1" minOccurs="1" name="in1" nillable="true" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
−
<xsd:element name="sayHelloWorldResponse">
−
<xsd:complexType>
−
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
−
<wsdl:message name="sayHelloWorldRequest">
<wsdl:part name="parameters" element="tns:sayHelloWorld">
    </wsdl:part>
</wsdl:message>
−
<wsdl:message name="sayHelloWorldResponse">
<wsdl:part name="parameters" element="tns:sayHelloWorldResponse">
    </wsdl:part>
</wsdl:message>
−
<wsdl:portType name="HelloWorldServicePortType">
−
<wsdl:operation name="sayHelloWorld">
<wsdl:input name="sayHelloWorldRequest" message="tns:sayHelloWorldRequest">
    </wsdl:input>
<wsdl:output name="sayHelloWorldResponse" message="tns:sayHelloWorldResponse">
    </wsdl:output>
</wsdl:operation>
</wsdl:portType>
−
<wsdl:binding name="HelloWorldServiceHttpBinding" type="tns:HelloWorldServicePortType">
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
−
<wsdl:operation name="sayHelloWorld">
<wsdlsoap:operation soapAction=""/>
−
<wsdl:input name="sayHelloWorldRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
−
<wsdl:output name="sayHelloWorldResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
−
<wsdl:service name="HelloWorldService">
−
<wsdl:port name="HelloWorldServiceHttpPort" binding="tns:HelloWorldServiceHttpBinding">
<wsdlsoap:address location="http://localhost:8888/xfiredemo/services/HelloWorldService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
 

二,WSDL的基本讲解

 

上面就是一个sayHelloWorld与add两个服务的WSDL,对于上面那段WSDL在这里简单的介绍一下。


从下往上看

1 Service- 相关服务访问点的集合:通过它你可以知道,你将通过哪个URL去访问你需要访问的的服务

元素包含一个或者多个Port元素 

2 Port-每一个Port元素对应一个不同的Web服务,port将一个URL赋予一个特定的binding,通过location实现 可以使两个或者多个port元素将不同的URL赋给相同的binding

3 binding无素:特定端口类型的具体协议和数据格式规范的绑定

4 Message元素描述了Web服务的有效负载。相当于函数调用中的参数和返回值:

5 PortType - 对于某个访问入口点类型所支持的操作的抽象集合,这些操作可以由一个或多个服务访问点来支持。

6 Operation - 对服务中所支持的操作的抽象描述,一般单个Operation描述了一个访问入口的请求/响应消息对。

7 Message - 通信消息的数据结构的抽象类型化定义。使用Types所定义的类型来定义整个消息的数据结构:相当于方法调用的参数与返回值定义

 

8 Types - 数据类型定义的容器,它使用某种类型系统(一般地使用XML Schema中的类型系统)。:从我给出的例子中,你会发现,那些message最终都定义在types中。比如sayHelloWorld 输入两个参数,一个String,一个int,返回String, add服务输入两个int,返回一个int等

 

上面那些WSDL基本元素第一次看不懂,无所谓,你试着去改变你的服务(JAVA),然后再发布,再看相应的WSDL

 

如此重复几次,你就明白了。正所谓书读百遍,其义自现,这样比死记硬背好多了。

 

 

 

三,客户端怎么调用服务

 

用代码说明问题:

com.dxm.cyou.xfire.test.XFireTest.java



package com.dxm.cyou.xfire.test;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.codehaus.xfire.client.Client;

public class XFireTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub

		XFireTest xFireTest = new XFireTest();
		xFireTest.test();
	}
	
	
	public void test() throws  Exception{
		
		Client client = new Client(new URL("http://localhost:8888/xfiredemo//services/HelloWorldService?wsdl"));
		Object[] results = client.invoke("sayHelloWorld", new Object[] {"邓小明", 100}); 
		System.out.println("results:" + results[0]); 
		
		Object[] results2 = client.invoke("add", new Object[] {100,	200}); 
		System.out.println("results:" + results2[0]); 
	}

}

 

    结果如下:


results:Hello World:邓小明||age:24
100 + 200 : 300

 

That's all

说明一下,今天第一次接触这个东东。以前老师讲过axis2基本全忘记了。
 
1
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics