eclipse(3.7.0)+tomcat(6.0)+xfire(1.2.6) webservice的服务器与客户端开发

初次接触webservice开发,在部署上遇到了不少问题,现整理如下:

webservice 简单来说就是使自己的应用程序成为web程序。

开始一直很迷茫webservice与javaweb有什么区别,至写本文档时仍让感觉不很清晰,希望看到的大侠能给以解答。好了废话不说了,以下是正文!

一、服务器端开发

1.下载xfire:

http://xfire.codehaus.org/Download,下载其最新的二进制版本,对xfire有兴趣,英语牛逼的可以下载源代码版,本人下载的是1.2.6版。

2.利用eclipse+xfire开发webservice服务端

2.1在eclipse中new一个Dynamic Web Project,假定项目名为xfireDemo,假设content的名,命名空间为xfire

2.2解压下载的xfire zip包,将xfire-1.2.6目录下的xfire-all-1.2.6.jar和xfire-1.2.6\lib下的所有jar文件拷贝到xfireDemo项目中的webcontent\web-inf\lib目录下,并将其导入项目的buildpath中。

2.3修改webcontent\web-inf\web.xml配置文件的内容,下面是修改后的web.xml文件。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>newXfire</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
         <servlet-name>XFireServlet</servlet-name>
         <servlet-class>
                 org.codehaus.xfire.transport.http.XFireConfigurableServlet
         </servlet-class>
     </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>
</web-app>

 web.xml配置文件中的servlet映射表明,所有匹配“/services/*”的请求全部交给

org.codehaus.xfire.transport.http.XFireConfigurableServlet

来处理。

2.4编写需要发布为webservice的java类,本例以最简单的一个加法类MathService.java为例:

package com.hengbao.servlet;
/**
 * @author 作者 guwh
 * @version 创建时间:2012-2-23 下午2:39:38
 * 类说明
 */
public class MathService {
	public int Add(int a ,int b){
        return a+b ;
    }
}

 2.5在webcontent\meta-inf目录下新建xfire文件夹,然后在xfire文件夹下添加一个xfire使用的service.xml文件,该配置文件的内容反映了要将那些java类发布为webservice服务。本例中的service.xml文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
    <service>
      <name>MathService</name>
      <namespace>http://com.hengbao.servlet/MathService</namespace>
      <serviceClass>com.hengbao.servlet.MathService</serviceClass>
    </service>
</beans>

 xfire会借助spring来解析service.xml文件。从中提取要分布为webservice配置的信息。

2.6在webcontent\web-inf目录下新建classes文件夹,然后将webcontent下的meta-inf文件全部剪贴到(move)此classes文件夹下。

否则会报:

“org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [META-INF/xfire/services.xml]; nested exception is java.io.FileNotFoundException: class path resource [META-INF/xfire/services.xml] cannot be opened because it does not exist”。

 的错误。

2.7将本项目发布到tomcat下,其中tomcat,在浏览器中输入:http://localhost:8080/xfire/services/MathService?wsdl会得到正确的web服务描述文档。

至此,webservice的服务器端代码开发完毕。

二、客户端开发:

据说客户端开发的方案有很多种,在此本人采用eclipse+xfire插件的方式开发。

1.xfire-eclispe插件安装:

打开eclispe--help--install new software,add Name:(自己填,如xfire);Location:填入网址:http://dist.codehaus.org/xfire/update/   详细插件安装过程见:http://xfire.codehaus.org/Eclipse+Plugin

2.插件安装成功后:

在eclipse中新建一个Java Project,假设项目名为:xfireTest。

3.在本项目中,new一个XFire--Code generation from WSDL document,点击next,在WSDL URL or path中输入服务器端wsdl的访问网址:本例为http://localhost:8080/xfire/services/MathService?wsdl ,在Output directory中填入:/xfireTest/src,在Package中写入你自己定义的包名:本例中写的是com.hengbao.cilent,点击finish完成。则eclipse会自动生成一个java类和其他的一写文件。本例中生成的java类是在com.hengbao.cilent包下的

MathServiceClient.java
MathServiceImpl.java
MathServicePortType.java

和servlet.hengbao.com.mathservice包下的

Add.java
AddResponse.java
ObjectFactory.java
package-info.java

 4.在

MathServiceClient.java

类中新增一下main代码,以进行测试,代码如下:

public class MathServiceClient {

   ........
    //新增main测试代码
    public static void main(String[] args) {
      //new一个客户端
        MathServiceClient client = new MathServiceClient();
        //创建服务
        MathServicePortType service = client.getMathServiceHttpPort();
    	//调用服务
    	System.out.println(service.add(4, 5));
	}

}

其中webservice服务器端,然后运行程序后,在控制台输出“9”,这边明调用web服务成功。

如果在运行客户端程序时,报:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/httpclient/methods/RequestEntity
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.getConstructor(Unknown Source)
at org.codehaus.xfire.transport.http.HttpChannel.sendViaClient(HttpChannel.java:108)
at org.codehaus.xfire.transport.http.HttpChannel.send(HttpChannel.java:48)
at org.codehaus.xfire.handler.OutMessageSender.invoke(OutMessageSender.java:26)
at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131)
at org.codehaus.xfire.client.Invocation.invoke(Invocation.java:79)
at org.codehaus.xfire.client.Invocation.invoke(Invocation.java:114)
at org.codehaus.xfire.client.Client.invoke(Client.java:336)
at org.codehaus.xfire.client.XFireProxy.handleRequest(XFireProxy.java:77)
at org.codehaus.xfire.client.XFireProxy.invoke(XFireProxy.java:57)
at $Proxy8.hello(Unknown Source)
at life.com.hello.Client.<init>(Client.java:11)
at life.com.hello.Client.main(Client.java:15)

 的错误,则表明缺少jar包commons-httpclient-3.0.jar,添加jar后还报:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/codec/DecoderException
at org.apache.commons.httpclient.HttpMethodBase.<init>(HttpMethodBase.java:220)
at org.apache.commons.httpclient.methods.ExpectContinueMethod.<init>(ExpectContinueMethod.java:93)
at org.apache.commons.httpclient.methods.EntityEnclosingMethod.<init>(EntityEnclosingMethod.java:119)
at org.apache.commons.httpclient.methods.PostMethod.<init>(PostMethod.java:106)
at org.codehaus.xfire.transport.http.CommonsHttpMessageSender.open(CommonsHttpMessageSender.java:135)
at org.codehaus.xfire.transport.http.HttpChannel.sendViaClient(HttpChannel.java:121)
at org.codehaus.xfire.transport.http.HttpChannel.send(HttpChannel.java:48)
at org.codehaus.xfire.handler.OutMessageSender.invoke(OutMessageSender.java:26)
at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131)
at org.codehaus.xfire.client.Invocation.invoke(Invocation.java:79)
at org.codehaus.xfire.client.Invocation.invoke(Invocation.java:114)
at org.codehaus.xfire.client.Client.invoke(Client.java:336)
at org.codehaus.xfire.client.XFireProxy.handleRequest(XFireProxy.java:77)
at org.codehaus.xfire.client.XFireProxy.invoke(XFireProxy.java:57)
at $Proxy8.hello(Unknown Source)
at life.com.hello.Client.<init>(Client.java:11)
at life.com.hello.Client.main(Client.java:15)

 的错误,表明缺少commons-codec-1.3.jar,添加jar包后运行成功,输入“9”。

至此,xifre的webservice应用的服务器端与客户端开发部署完毕。

 

猜你喜欢

转载自gwh-08.iteye.com/blog/1420243