xfire开发webservice

 

Myeclipse5.X支持xfire支持的非常棒。这里讲解一个简单的例子,此文章为引用文章,仅做保留,供学习参考使用。

一、服务端程序开发

1、首先建立一个web service工程

 

 

 

 

 

 

 

 

 

 

点击finish之后,myeclipse自动帮你生成services.xml以及web应用目录结构,其中的services.xml是你导出服务的配置文件,注意在WEB-INF/web.xml文件中配置了xfire自己的:

Servlet代码

<servlet> 
 <servlet-name>XFireServlet</servlet-name>  

<servlet-class>

org.codehaus.xfire.transport.http.XFireConfigurableServlet

</servlet-class> 
 <load-on-startup>0</load-on-startup> 
</servlet> 
<servlet-mapping> 
 <servlet-name>XFireServlet</servlet-name> 
 <url-pattern>/services/*</url-pattern> 
</servlet-mapping

2、接下来,现在编写要导出的pojo类,首先是接口:

过程:File>New>Other>MyEclipse>Web Service

 

 

选择:Create web service from Java bean,把Create new javabean也勾上

 

 

3、在导航页面中,输入

web service name:HelloWorldTest

java package : services

services Interface : HelloWorldTestRemote

services impl.class : HelloWorldTestBean

其他的默认,点finish

 

 

最后可以看到Web Services   下面的services.xml的内容如下

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

       services.HelloWorldTestRemote

  </serviceClass>  

       <implementationClass>services.HelloWorldTestBean

  </implementationClass>
  <style>wrapped</style>
  <use>literal</use>
  <scope>application</scope>
 </service>
</beans>

4、services包中的文件

HelloWorldTestRemote.java

-----------------------------------------------

package services;

public interface HelloWorldTestRemote

{  

    public String example(String message); 

}

----------------------------------------------------------

HelloWorldTestRemote.java

----------------------------------------------------

package services;

public class HelloWorldTestBean implements HelloWorldTestRemote

{

    public String example(String message)

    {

        return "Hello:"+message;

    }  

}

------------------------------------------------------------

到此为止webServices编码工作完成,现在开始部署到tomcat这里省略。

部署好后可以测试下:

(1)查看可用的服务

http://localhost:8080/HelloWorld/services

(2)查看wsdl

http://localhost:8080/HelloWorld/services/HelloWorldTest?wsdl

 

二、客户端应用程序开发

1、新建工程useWebService,并且建立client和services包

2、bulid path中导入,XFire 1.1 core libraries 和XFire 1.1 HTTP client libraries.

xfire开发webservice

SayHello.java

----------------------------------------

package client;

import java.net.MalformedURLException;

import org.codehaus.xfire.XFireFactory;

import org.codehaus.xfire.client.XFireProxyFactory;

import org.codehaus.xfire.service.Service;

import org.codehaus.xfire.service.binding.ObjectServiceFactory;

import services.HelloWorldTestRemote;

public class SayHello

{

    public String say(String name)

    {

        String result = "";

Service srvcModel = new ObjectServiceFactory().create(HelloWorldTestRemote.class);

        XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());

        String helloWorldURL = "http://localhost:8080/HelloWorld/services/HelloWorldTest";

        try

        {

            HelloWorldTestRemote srvc = (HelloWorldTestRemote) factory.create(srvcModel, helloWorldURL);

            result = srvc.example(name);

            System.out.println(result);

        }

        catch (MalformedURLException e)

        {

            e.printStackTrace();

        } finally

        {

            return result;

        }

    }

}

--------------------------------------------------------------

HelloWorldTestRemote.java

---------------------------------------------------------------

package services;

 

public interface  HelloWorldTestRemote

{

    public String example(String message);

}-----------------------------------------------------------------

创建index.jsp,body部分

-----------------------

<form name="form1" action="say.jsp" method="POST">
您的大名:<input name="userName" type="text"/> <input value="提交" type="submit"/>
</form>

-------------------------------

创建say.jsp,body部分

----------------------------------------------------------------

 

<%
String name=(String)request.getParameter("userName");
String result=new SayHello().say(name);
out.print(result);
%>
webService远程调用结果: <%=result %>
------------------------------------------------------------------------
运行下测试!OK

 

你可能感兴趣的:(xfire)