用Xfire发布Webservice应用(转载)
开发的IDE我用的是Eclipse3.2,下面简单说一说用Xfire发布一个Webservice的步骤: (3)我们需要在TestConsole里面构建自己业务逻辑。这里我写了4个类,一个类是接口文件BusinessInterface.java,作为发布给客户端的业务方法接口,一个是具体的实现类BusinessObject.java,这个类是不用发布到客户端的,两外两个类一个是入口参数类TestVO.java,一个是出口参数类ReturnVO.java,设置这两个类的目的是为了模拟最一般的情形,代码分别如下: 该文件的具体内容如下:<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://xfire.codehaus.org/config/1.0"><service>??? <name>Business</name>??? <namespace>mytest</namespace>??? <serviceClass>com.zhiyuansoft.test.BusinessInterface</serviceClass>??? <implementationClass>com.zhiyuansoft.test.BusinessObject</implementationClass></service> </beans>其中serviceClass为对外的业务接口类,如果不是接口的话就直接写业务类,如果是接口的话下面还需要指明实现类,即implementationClass。到此为止,服务器端的功能就完成了,验证无误后我们可以把这个项目发布到服务器上,我用的中间件是Jboss4.0。服务器端的功能发布完成后,接下来就要写一个客户端来调用服务器端发布的这个Webservice。(5)新建一个工作区,建好后同样新建一个Web项目,名为ClientWeb,这个项目只是用来调用一个Webservice服务之用。将Xfire的库文件拷贝到Web项目的WEB-INF下面,同时需要将服务器端发布的服务打包发布给客户端,发布给客户端的代码只需要打包接口和参数类即可,实现类不必打包到客户端,类似于EJB的分发。(6)在这个Web项目里面新建一个Servlet作为测试的Servlet,我这里叫做ClientServlet.java,在web.xml里面配置好这个Servlet后,就可以在Servlet里面写Webservice调用的代码了。客户端的web.xml如下所示:========================<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" ??? xmlns="http://java.sun.com/xml/ns/j2ee" ??? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ??? xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee ??? http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><servlet>??? <description>This is the description of my J2EE component</description>??? <display-name>This is the display name of my J2EE component</display-name>??? <servlet-name>ClientServlet</servlet-name>??? <servlet-class>com.zhiyuansoft.servlet.ClientServlet</servlet-class></servlet><servlet-mapping>??? <servlet-name>ClientServlet</servlet-name>??? <url-pattern>/servlet/ClientServlet</url-pattern></servlet-mapping></web-app>==========================我们在这个Servlet里面增加一个方法如下(这个方法就是客户端调用服务器端Webservice的核心方法)??? public ReturnVO callWebService()??????? throws MalformedURLException,Exception {??? ??????? Service serviceModel = new ObjectServiceFactory().create(BusinessInterface.class);?????? ??????? XFire xfire = XFireFactory.newInstance().getXFire();??????? XFireProxyFactory factory = new XFireProxyFactory(xfire);???? ??????? String serviceUrl = "http://localhost:8080/TestWeb/services/Business";??????? BusinessInterface client = null;??????? try {??????????? client = (BusinessInterface) factory.create(serviceModel,serviceUrl);??????? } catch (MalformedURLException e) {??????? ??? e.printStackTrace();??????? }???????? ??????? //调用服务??????? ReturnVO serviceResponse = null;??????? try {??????? ??? int age=30;??????? ??? TestVO tvo=new TestVO();??????? ??? tvo.setName("test-name");??????? ??? tvo.setAge(age);??????????? serviceResponse = client.getTestString(tvo);?????? } catch (Exception e){??????????? e.printStackTrace();??????? }?????? ??????? return serviceResponse;??? } 这个方法我们通过在Servlet的doGet方法里调用来实现最终对Webservice的调用,代码如下:??? public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException{??? ??? response.setContentType("text/html");??? ??? PrintWriter out = response.getWriter();??? ??? out.println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">");??? ??? out.println("<HTML>");??? ??? out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");??? ??? out.println(" <BODY>");??? ??? out.print("??? the result of Call Webservice is ");??? ??? try{??? ??? ??? out.print(callWebService().getDescStr());??? ??? }catch(Exception e){??? ??? ??? e.printStackTrace();??? ??? }??? ??? out.println(",using the GET method");??? ??? out.println(" </BODY>");??? ??? out.println("</HTML>");??? ??? out.flush();??? ??? out.close();??? }整个Servlet的代码如下 ClientServlet.java:package com.zhiyuansoft.servlet;import java.io.IOException;import java.io.PrintWriter;import java.net.MalformedURLException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.codehaus.xfire.XFire;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 com.zhiyuansoft.test.BusinessInterface;import com.zhiyuansoft.test.BusinessObject;import com.zhiyuansoft.test.ReturnVO;import com.zhiyuansoft.test.TestVO;public class ClientServlet extends HttpServlet {??? /**??? * Constructor of the object.??? */??? public ClientServlet() {??? ??? super();??? }??? /**??? * Destruction of the servlet. <br>??? */??? public void destroy() {??? ??? super.destroy(); // Just puts "destroy" string in log??? ??? // Put your code here??? }??? /**??? * The doDelete method of the servlet. <br>??? *??? * This method is called when a HTTP delete request is received.??? * ??? * @param request the request send by the client to the server??? * @param response the response send by the server to the client??? * @throws ServletException if an error occurred??? * @throws IOException if an error occurred??? */??? public void doDelete(HttpServletRequest request,??? ??? ??? HttpServletResponse response) throws ServletException,IOException {??? ??? // Put your code here??? }??? /**??? * The doGet method of the servlet. <br>??? *??? * This method is called when a form has its tag value method equals to get.??? * ??? * @param request the request send by the client to the server??? * @param response the response send by the server to the client??? * @throws IOException ??? * @throws Exception ??? */??? public void doGet(HttpServletRequest request,using the GET method");??? ??? out.println(" </BODY>");??? ??? out.println("</HTML>");??? ??? out.flush();??? ??? out.close();??? }??? /**??? * The doPost method of the servlet. <br>??? *??? * This method is called when a form has its tag value method equals to post.??? * ??? * @param request the request send by the client to the server??? * @param response the response send by the server to the client??? * @throws ServletException if an error occurred??? * @throws IOException if an error occurred??? */??? public void doPost(HttpServletRequest request,HttpServletResponse response)??? ??? ??? throws ServletException,IOException {??? ??? response.setContentType("text/html");??? ??? PrintWriter out = response.getWriter();??? ??? out??? ??? ??? ??? .println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">");??? ??? out.println("<HTML>");??? ??? out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");??? ??? out.println(" <BODY>");??? ??? out.print("??? This is ");??? ??? out.print(this.getClass());??? ??? out.println(",using the POST method");??? ??? out.println(" </BODY>");??? ??? out.println("</HTML>");??? ??? out.flush();??? ??? out.close();??? }??? /**??? * The doPut method of the servlet. <br>??? *??? * This method is called when a HTTP put request is received.??? * ??? * @param request the request send by the client to the server??? * @param response the response send by the server to the client??? * @throws ServletException if an error occurred??? * @throws IOException if an error occurred??? */??? public void doPut(HttpServletRequest request,IOException {??? ??? // Put your code here??? }??? /**??? * Returns information about the servlet,such as ??? * author,version,and copyright. ??? *??? * @return String information about this servlet??? */??? public String getServletInfo() {??? ??? return "This is my default servlet created by Eclipse";??? }??? /**??? * Initialization of the servlet. <br>??? *??? * @throws ServletException if an error occure??? */??? public void init() throws ServletException {??? ??? // Put your code here??? }??? /* Call the Web service??? *??? */??? public ReturnVO callWebService()??????? throws MalformedURLException,serviceUrl);??????? } catch (MalformedURLException e) {??????? ??? e.printStackTrace();??????? }???????? ??????? //Invoke the service??????? ReturnVO serviceResponse = null;??????? try {??????? ??? int age=30;??????? ??? TestVO tvo=new TestVO();??????? ??? tvo.setName("test-name");??????? ??? tvo.setAge(age);??????????? serviceResponse = client.getTestString(tvo);?????? } catch (Exception e){??????????? e.printStackTrace();??????? }?????? ??????? return serviceResponse;??? } }(7)同样将这个Web项目发布,然后在地址栏里面输入:http://localhost:8080/ClientWeb/servlet/ClientServlet,此时会调用客户端的Servlet,然后调用其doGet方法,如果正常的话,可以看到页面输出结果为:the result of Call Webservice is the test-name's age is 30,using the GET method在这里我们看到,服务器端正确的完成了我们的业务操作,即将入参里面的名字和年龄组装成一句话返回。俗话说,麻雀虽小,五脏俱全,这个例子虽然很简单,但是完整的展示了一个最简单的Xfrie发布一个Wevservice应用的过程。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |