XFire传入传出自定义类型和集合类型学习笔记

 

工程结构图

 

首先建立一个web工程作为Service端,在classpath中加载XFire Jar包,并编写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 >
      
< servlet-name > XFire Servlet </ servlet-name >
      
< servlet-class > org.codehaus.xfire.transport.http.XFireConfigurableServlet </ servlet-class >
      
< load-on-startup > 0 </ load-on-startup >
    
</ servlet >
    
< servlet-mapping >
      
< servlet-name > XFire Servlet </ servlet-name >
      
< url-pattern > /services/* </ url-pattern >
    
</ servlet-mapping >
  
< welcome-file-list >
    
< welcome-file > index.jsp </ welcome-file >
  
</ welcome-file-list >
</ web-app >

 

编写Service接口:

 

package  Test;

import  java.util.List;

public   interface  IHelloWorldService  {
  
public User HelloWorld(User user); //传入和返回自定义类型
  public List HelloWorldList(); //返回集合类型
}

编写Service实现类:

package  Test;
import  java.util.ArrayList;
import  java.util.List;
public   class  HelloWorldServiceImpl  implements  IHelloWorldService  {

    
public User HelloWorld(User user) {
      
if(user.getUsername().equals("gaoxiang")){
          
return new User("welcome gaoxiang","1234");
      }

      
else{
          
return new User("wrong name","1234");
      }

    
    }


    
public List HelloWorldList() {
        
        List a
=new ArrayList();
        User u1
=new User("1","1");
        User u2
=new User("2","2");
        User u3
=new User("3","3");
        a.add(u1);
        a.add(u2);
        a.add(u3);
        
return a;
    }


}

为返回类型和传入类型配置aegis映射IHelloWorldService.aegis.xml

<? xml version = " 1.0 "  encoding = " UTF-8 " ?>
< mappings >
    
< mapping >
        
< method name = " HelloWorld " >
            
< parameter index = " 0 "  componentType = " Test.User " />
        
</ method >
  
        
< method name = " HelloWorld " >
            
< return - type componentType = " Test.User " />
        
</ method >
        
< method name = " HelloWorldList " >
            
< return - type componentType = " Test.User " />    <!--  定义返回集合类型中元素的type  -->
        
</ method >
    
</ mapping >
</ mappings >

 

在src目录下建立META-INF/xfire目录,并在其编写service.xml

 

< beans >
<!--   第一种方式配置
<service xmlns="http://xfire.codehaus.org/config/1.0">
<name>HelloService</name>
  <namespace>http://xfire.sttudy/HelloService</namespace>
  <serviceClass>Test.IHelloWorldService</serviceClass>
  <implementationClass>Test.HelloWorldServiceImpl</implementationClass>
</service>
-->
<!--  使用Spring方式配置  -->
< bean  name ="HelloService"  class ="org.codehaus.xfire.spring.ServiceBean" >
 
< property  name ="serviceBean"  ref ="echo" />
 
< property  name ="serviceClass"  value ="Test.IHelloWorldService" />
< property  name ="inHandlers" >
< list >
< ref  bean ="addressingHandler" />
</ list >
</ property >
</ bean >
< bean  id ="echo"  class ="Test.HelloWorldServiceImpl" />
< bean  id ="addressingHandler"  class ="org.codehaus.xfire.addressing.AddressingInHandler" />
</ beans >

User.java

 

package  Test;

public   class  User  {
   
private String username;
   
private String password;
   
public User(){
       
   }

   
public User(String username,String password){
       
this.username=username;
       
this.password=password;
       
   }

public String getPassword() {
    
return password;
}

public void setPassword(String password) {
    
this.password = password;
}

public String getUsername() {
    
return username;
}

public void setUsername(String username) {
    
this.username = username;
}

}

 


测试WSDL:

http://localhost:8080/XFireStudy/services/IHelloWorldService?wsdl

 

新建Client工程

使用MyEclipse的自动生成Web Service Client功能(file-new-other-myeclipse-web service-web service client)生成Client代码,需要说明的是,也会生成一个User类,但这个类不是服务端的POJO,而是通过JAXB2绑定的,稍微复杂,读取方式由原来的getUsername()转换成getUsername().getValue();因为getUsername()返回的是一个JAXBElement对象

编写测试代码:

 

package  client;

import  java.util.List;

import  javax.xml.bind.JAXBElement;
import  javax.xml.namespace.QName;

import  test.IHelloWorldServiceClient;
import  test.IHelloWorldServicePortType;
import  test.User;



public   class  TestWS  {

    
/**
     * 
@param args
     
*/

    
public static void main(String[] args) {
        IHelloWorldServiceClient client
=new IHelloWorldServiceClient();
        IHelloWorldServicePortType helloService
=client.getIHelloWorldServiceHttpPort();
        
        
//构造user对象稍微复杂些,因为使用了JAXB2绑定,其中QName()参数分别是targetNameSpace和property name
        User user = new User();
        JAXBElement
<String> name = new JAXBElement<String>(new
        QName(
"http://Test""username"),String.class,"gaoxiang");
        JAXBElement
<String> password = new JAXBElement<String>(new
        QName(
"http://Test""password"),String.class,"1234");
        user.setUsername(name);
        user.setPassword(password);
        System.out.println(helloService.helloWorld(user).getUsername().getValue());
        
        
        List
<User> a=helloService.helloWorldList().getUser();
        
for(User u1:a){
            System.out.println(u1.getUsername().getValue());
        }

    }


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值