webservice发布及远程调用

Web Services是由企业发布的完成其特定商务需求的在线应用服务,其他公司或应用软件能够通过Internet来访问并使用这项在线服务。用简单点的话说,就是系统对外的接口!

Web Service是现在最适合实现SOA的技术,而Axis2XFire是实现Web Service的两种技术框架(架构)。

SOA是面向服务的体系结构(Service-Oriented ArchitectureSOA)是一个组件模型,它将应用程序的不同功能单元(称为服务)通过这些服务之间定义良好的接口和契约联系起来。接口是采用中立的方式进行定义的,它应该独立于实现服务的硬件平台、操作系统和编程语言。这使得构建在各种各样的系统中的服务可以使用一种统一和通用的方式进行交互。

相对于Axis来说,目前XFire相对受欢迎,加上其提供了和Spring集成的支持,在目前的Web Service开源社区拥有众多的追随者。

 XFirecodeHaus组织提供的一个开源框架,它构建了POJOSOA之间的桥梁,主要特性就是支持将POJO通过非常简单的方式发布成Web服务,这种处理方式不仅充分发挥了POJO的作用,简化了Java应用转化为Web服务的步骤和过程,也直接降低了SOA的实现难度,为企业转向SOA架构提供了一种简单可行的方式。

服务器端发布Webservice接口

在你的项目web.xml文件中,增加以下配置

<servlet>   

     <servlet-name>xfire</servlet-name>   
     
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>   
<servlet-mapping> 
      
<servlet-name>xfire</servlet-name>
      
<url-pattern>*.ws</url-pattern>
</servlet-mapping>
<servlet>
      
<!-- 配合Spring容器中XFire一起工作的Servlet-->
      
<servlet-name>xfireServlet</servlet-name>
      
<servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
</servlet>
<servlet-mapping>

      <servlet-name>xfireServlet</servlet-name>

      <!-- 在这个URI下开放Web Service服务 -->
      
<url-pattern>/service/*</url-pattern>
</servlet-mapping>

定义HelloWorld接口和其实现类HelloWorldImpl,实现sayHello方法。

publicclass HelloWorldImpl implements HelloWorld {

public String sayHello(String name) {

       String helloWorld = "hello," + name;

       return helloWorld;

    }
}

XFireSpring提供了方便易用的导出器XFireExporter,借助该导出器的支持,我们可以在Spring容器中将一个POJO导出为Web ServiceHelloWorld是业务服务类,在此拥有一个sayHello的方法,我们希望将此方法开放为Web Service。(POJOPlain Old Java Object简单Java对象)

xfire-servlet.xml文件中导出器的设置,该文件内容如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
   "http://www.springframework.org/dtd/spring-beans.dtd"
>
<beans>
    
<!-- 引入XFire预配置信息 -->
    
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
    
<!-- 使用XFire导出器 -->
    
<bean id="baseWebService" class="org.codehaus.xfire.spring.remoting.XFireExporter" lazy-init="false" abstract="true">
       
<!-- 引用xfire.xml中定义的工厂 -->
       
<property name="serviceFactory" ref="xfire.serviceFactory" />
       
<!-- 引用xfire.xml中的xfire实例 -->
       
<property name="xfire" ref="xfire" />
    
</bean>
    
<bean id="HelloWorldService" parent="baseWebService">
       
<!-- 业务服务bean -->
       
<property name="serviceBean" ref="HelloWorldBean" />
       
<!-- 业务服务bean的窄接口类 -->
       
<property name="serviceClass" value="webservice.HelloWorld" />
    
</bean>
</beans>

当然自己的业务HelloWorldBean也需要配置下,在applicationContext.xml中,在此不细说。

<bean id="HelloWorldBean" class="com.webservice.HelloWorldImpl">

</bean>

在实际应用中,如果某个类具有众多的方法,而其中的某些方法不需要开放为Web Service的情况下,我们可以定义一个窄接口,该接口中只需定义那些开放为Web Service的业务方法。就是上述配置文件中的<property name="serviceClass" value="webservice.HelloWorld" />

此时webservice的配置已经全部完成,可以通过访问http://localhost:8080/projectName/HelloWorldService.ws?wsdl来查看发布的WSDl文档。

 

远程调用webservice

使用XFire

Client client = new Client(new URL("http://localhost:8080/projectName/service/HelloWorld?wsdl"));

/**

*invoke参数解析

*param1:调用服务方发布接口中的方法名

*param2:参数数组,可以传递多个参数,但是接口会根据实际需要参数从数组中一次取得(顺序)

*/

String str = "XFile";

Object[] obj = client.invoke("sayHello", new String[]{str});

String result = obj[0].toString();

System.out.println(result);

 

使用Axis2

String url = "http://localhost:8080/projectName/service/HelloWorld";

Service server = new Service();

Call call = (Call) server.createCall();

call.setTargetEndpointAddress(new URL(url));

String result = (String) call.invoke("sayHello", new String[]{"Axis2"});

System.out.println(result);

 

客户端调用webservice的方法网上有很多例子,在我的项目中也没有用到,在此就不多说了。

升级版的CXF还没用过,以后用到的时候再研究下。

 

该文章有一部分来自于:http://www.blogjava.net/amigoxie/archive/2007/09/26/148207.html

猜你喜欢

转载自yuanbian007.iteye.com/blog/1767919