首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

XFire框架兑现WebService(二)

2012-08-02 
XFire框架实现WebService(二)有了XFire框架实现WebService(一),就可以继续开发WebService的简单应用。Webse

XFire框架实现WebService(二)
   有了XFire框架实现WebService(一),就可以继续开发WebService的简单应用。
Webservice的服务端(WEB工程):
两个java bean类:
Course.java
   package cn.com.bean;
public class Course {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
         this.name = name;
    }
}

User.java
package cn.com.bean;
public class User {
      private String name;
      public String getName() {
          return name;
      }
      public void setName(String name) {
           this.name = name;
      }

}

一个Service接口入口: IHelloService.java
package cn.com.service;

import java.util.List;

import cn.com.bean.Course;
import cn.com.bean.User;

public interface IHelloService {
        public String sayHello(String t);
        public Course choose(User u);
        public List test(List t);
}

接口的实现类: HelloServiceImpl.java

package cn.com.service;

import java.util.ArrayList;
import java.util.List;
import cn.com.bean.Course;
import cn.com.bean.User;
public class HelloServviceImpl implements IHelloService {
       public Course choose(User u){
               System.out.println(u.getName());
               Course c = new Course();
               c.setName("Computer");
               return c;
       }
        public String sayHello(String t) {
               return "Hello,"+t;
        }
        public List test(List t) {
               for(int i=0;i<t.size();i++){
                    System.out.println((String)t.get(i));
               }
                List list = new ArrayList();
                Course c = new Course();
                c.setName("English Reading");
                list.add(c);
                return list;
        }
}
WebSevice定义描述文件: services.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
    <service>
        <name>HelloService</name>
        <namespace>my/HelloService</namespace>
        <serviceClass>cn.com.service.IHelloService</serviceClass>
        <implementationClass>cn.com.service.HelloServviceImpl</implementationClass>        
    </service>
</beans>

因为我们用到了List等集合类型,所以需要定义Mapping关系,文件名为:IHelloService.aegis.xml
<?xml version="1.0" encoding="UTF-8"?>
<mappings>
   <mapping>
      <method name="test">
         <parameter index="0" componentType="java.lang.String" />
         <return-type componentType="cn.com.bean.Course"/>
      </method>
   </mapping>
</mappings>

最后Web.xml文件添加XFire需要用到的Servlet:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <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>/servlet/XFireServlet/*</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>XFireServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

客户端代码(一个java工程即可):
package test;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

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 cn.com.bean.Course;
import cn.com.bean.User;
import cn.com.service.IHelloService;

public class ClientTest {

         public static void main(String[] args) throws MalformedURLException {
          //Service:是一个服务端点,它的工作是处理XML消息
         Service srvcModel = new ObjectServiceFactory().create(IHelloService.class);
             /*XFireProxyFactory:
      *  该工厂用来创建XFire SOAP客户端的远程对象,返回的实例将调用
      *  远程对象的所有方法
      */
     XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
    
          String helloWorldURL = "http://localhost:8088/testWebService2/services/HelloService";
        
         IHelloService service = (IHelloService)factory.create(srvcModel,helloWorldURL);
        
         System.out.println(service.sayHello("Robin"));
        
         User u = new User();
         u.setName("RRRRR");
         Course c = service.choose(u);
        
         System.out.println(c.getName());

         List list = new ArrayList();
         list.add("test1");
         list.add("test2");
        
         List tempList = service.test(list);
         for(int i=0;i<tempList.size();i++){
        Course course = (Course)tempList.get(i);
        System.out.println(course.getName());
         }
}

}



热点排行