实例中包括三个情况,我想基本上可以概括所有的需求,或者自己稍加扩展即可。先来看看我们的Interface。
![](../../../Images/OutliningIndicators/None.gif)
2
![](../../../Images/OutliningIndicators/None.gif)
3
![](../../../Images/OutliningIndicators/None.gif)
4
![](../../../Images/OutliningIndicators/None.gif)
5
![](../../../Images/OutliningIndicators/ExpandedBlockStart.gif)
![](../../../Images/OutliningIndicators/ContractedBlock.gif)
![](../../../Images/dot.gif)
6
![](../../../Images/OutliningIndicators/InBlock.gif)
7
![](../../../Images/OutliningIndicators/InBlock.gif)
8
![](../../../Images/OutliningIndicators/InBlock.gif)
9
![](../../../Images/OutliningIndicators/InBlock.gif)
10
![](../../../Images/OutliningIndicators/InBlock.gif)
11
![](../../../Images/OutliningIndicators/ExpandedBlockEnd.gif)
这其中包含了简单对象的传递,对象的传递,List的传递。
具体的开发步骤如下:
1、定义Web Service的接口,代码见上面的接口定义。
2、实现接口和业务逻辑,代码如下:
![](../../../Images/OutliningIndicators/None.gif)
2
![](../../../Images/OutliningIndicators/None.gif)
3
![](../../../Images/OutliningIndicators/None.gif)
4
![](../../../Images/OutliningIndicators/None.gif)
5
![](../../../Images/OutliningIndicators/None.gif)
6
![](../../../Images/OutliningIndicators/ExpandedBlockStart.gif)
![](../../../Images/OutliningIndicators/ContractedBlock.gif)
![](../../../Images/dot.gif)
7
![](../../../Images/OutliningIndicators/InBlock.gif)
8
![](../../../Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](../../../Images/OutliningIndicators/ContractedSubBlock.gif)
![](../../../Images/dot.gif)
9
![](../../../Images/OutliningIndicators/InBlock.gif)
10
![](../../../Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
11
![](../../../Images/OutliningIndicators/InBlock.gif)
12
![](../../../Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](../../../Images/OutliningIndicators/ContractedSubBlock.gif)
![](../../../Images/dot.gif)
13
![](../../../Images/OutliningIndicators/InBlock.gif)
14
![](../../../Images/OutliningIndicators/InBlock.gif)
15
![](../../../Images/OutliningIndicators/InBlock.gif)
16
![](../../../Images/OutliningIndicators/InBlock.gif)
17
![](../../../Images/OutliningIndicators/InBlock.gif)
18
![](../../../Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
19
![](../../../Images/OutliningIndicators/InBlock.gif)
20
![](../../../Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](../../../Images/OutliningIndicators/ContractedSubBlock.gif)
![](../../../Images/dot.gif)
21
![](../../../Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](../../../Images/OutliningIndicators/ContractedSubBlock.gif)
![](../../../Images/dot.gif)
22
![](../../../Images/OutliningIndicators/InBlock.gif)
23
![](../../../Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
24
![](../../../Images/OutliningIndicators/InBlock.gif)
25
![](../../../Images/OutliningIndicators/InBlock.gif)
26
![](../../../Images/OutliningIndicators/InBlock.gif)
27
![](../../../Images/OutliningIndicators/InBlock.gif)
28
![](../../../Images/OutliningIndicators/InBlock.gif)
29
![](../../../Images/OutliningIndicators/InBlock.gif)
30
![](../../../Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
31
![](../../../Images/OutliningIndicators/ExpandedBlockEnd.gif)
用到的User和Course两个类的代码如下:
![](../../../Images/OutliningIndicators/None.gif)
2
![](../../../Images/OutliningIndicators/None.gif)
3
![](../../../Images/OutliningIndicators/ExpandedBlockStart.gif)
![](../../../Images/OutliningIndicators/ContractedBlock.gif)
![](../../../Images/dot.gif)
4
![](../../../Images/OutliningIndicators/InBlock.gif)
5
![](../../../Images/OutliningIndicators/InBlock.gif)
6
![](../../../Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](../../../Images/OutliningIndicators/ContractedSubBlock.gif)
![](../../../Images/dot.gif)
7
![](../../../Images/OutliningIndicators/InBlock.gif)
8
![](../../../Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
9
![](../../../Images/OutliningIndicators/InBlock.gif)
10
![](../../../Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](../../../Images/OutliningIndicators/ContractedSubBlock.gif)
![](../../../Images/dot.gif)
11
![](../../../Images/OutliningIndicators/InBlock.gif)
12
![](../../../Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
13
![](../../../Images/OutliningIndicators/ExpandedBlockEnd.gif)
14
![](../../../Images/OutliningIndicators/None.gif)
![](../../../Images/OutliningIndicators/None.gif)
2
![](../../../Images/OutliningIndicators/None.gif)
3
![](../../../Images/OutliningIndicators/ExpandedBlockStart.gif)
![](../../../Images/OutliningIndicators/ContractedBlock.gif)
![](../../../Images/dot.gif)
4
![](../../../Images/OutliningIndicators/InBlock.gif)
5
![](../../../Images/OutliningIndicators/InBlock.gif)
6
![](../../../Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](../../../Images/OutliningIndicators/ContractedSubBlock.gif)
![](../../../Images/dot.gif)
7
![](../../../Images/OutliningIndicators/InBlock.gif)
8
![](../../../Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
9
![](../../../Images/OutliningIndicators/InBlock.gif)
10
![](../../../Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](../../../Images/OutliningIndicators/ContractedSubBlock.gif)
![](../../../Images/dot.gif)
11
![](../../../Images/OutliningIndicators/InBlock.gif)
12
![](../../../Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
13
![](../../../Images/OutliningIndicators/InBlock.gif)
14
![](../../../Images/OutliningIndicators/ExpandedBlockEnd.gif)
3、编写XFire要求的WebSevice定义描述文件,如下:
![](../../../Images/OutliningIndicators/None.gif)
2
![](../../../Images/OutliningIndicators/None.gif)
3
![](../../../Images/OutliningIndicators/None.gif)
4
![](../../../Images/OutliningIndicators/None.gif)
5
![](../../../Images/OutliningIndicators/None.gif)
6
![](../../../Images/OutliningIndicators/None.gif)
7
![](../../../Images/OutliningIndicators/None.gif)
8
![](../../../Images/OutliningIndicators/None.gif)
9
![](../../../Images/OutliningIndicators/None.gif)
10
![](../../../Images/OutliningIndicators/None.gif)
11
![](../../../Images/OutliningIndicators/None.gif)
此文件放在src/META-INF/xfire/services.xml,编译时会自动编译到classes的相应目录下面。
1. 如果Web Services的方法的参数是Collections,那就需要建一个Mapping文件,而且Mapping文件的命名为className.aegis.xml,并且与class放在同一个package下。
如:
public interface IConstraceSerice {
boolean editConstraceInfo(List aList);
}
Mapping文件如下:IConstraceService.aegis.xml
<?xml version="1.0" encoding="utf-8"?>
<mappings>
<mapping>
<method name="editConstraceInfo">
<parameter index="0" componentType="java.lang.String"/>
</method>
</mapping>
</mappings>
<parameter index="0" componentType="java.lang.String"/>表示第一个参数,里面实际值的类型,这里实际值的类型是java.lang.String.
如果是一个JavaBean,如com.test.TestBean,那以就要写成<parameter index="0" compentType="com.test.TestBean"/>
2. 如果返回类型是List或Map,并且里面存放的是自定义类的话,则需要增加一个对于服务接口的配置文件。该文件的命名规则是 接口文件名.aegis.xml。例如接口是UserService.java的话,则此配置文件命名为UserService.aegis.xml。注意此配置文件须与接口放在同一目录下面。
<?xml version="1.0" encoding="UTF-8"?>
<mappings>
<mapping >
<method name="getUsers">
<return-type componentType="com.test.domain.User"/>
</method>
</mapping>
</mappings>
getUsers方法返回类型是List,里面装的User对象。对于这种类型的方法,在配置文件中描述了它的返回值类型。
如果返回的类型是Map的话,做法和List一样。但定义的类型,是Map中的Value部分,并且这样的话,Map中Value所存放的对象就必须全部是同一种类啦。
下面给出一个详细的例子:
1)服务接口:
public interface MyService2
{
boolean getInfo();
Collection getCollection(); //method 1
Collection getCollection(int id); //method 2
Collection getCollection(String id); //method 3
Collection getCollectionForValues(String id, Collection c); //method 4
Collection getCollectionForValues(int value, Collection c); //method 5
}
2) Mapping文件内容:
<mappings>
<mapping>
<!-- mapping 1 -->
<method name="getCollection">
<return-type componentType="java.lang.Double"/>
</method>
<!-- mapping 2 -->
<method name="getCollection">
<return-type componentType="java.lang.Float"/>
<parameter index="0" class="int"/>
</method>
<!-- mapping 3 -->
<method name="getCollectionForValues">
<return-type componentType="java.math.BigDecimal"/>
</method>
<!-- mapping 4 -->
<method name="getCollectionForValues">
<parameter index="0" class="java.lang.String"/>
<parameter index="1" componentType="java.util.Date"/>
</method>
<!-- mapping 5 -->
<method name="getCollectionForValues">
<return-type componentType="java.util.Calendar"/>
<parameter index="0" class="int"/>
<parameter index="1" componentType="java.lang.Bit"/>
</method>
</mapping>
</mappings>
3. 如果一个方法的返回类型是一个JavaBean,而这个JavaBean当中又存在Collections,那么就需要定义一个与JavaBean相关的Mapping文件,文件名要与JavaBean名相同,如:User.aegis.xmll,并且与JavaBean放在同一个目录.
例子:
1) 服务接口
public interface IYMServiceFacade {
User getUser();
}
2) JavaBean
public class User {
private Strirng userName;
// 这里是一个Collection
private Set rooms;
.....
.....
}
3) Mapping文件(User.aegis.xml)
<?xml version="1.0" encoding="utf-8"?>
<mappings>
<mapping>
<property name="rooms" componentType="com.powerunion.ymservice.dto.Room"/>
</mapping>
</mappings>
介绍:<property name="rooms" componentType="com.powerunion.ymservice.dto.Room"/>
其中的name属性就是JavaBean里面定义的rooms,componentType上面的相同,表示Collections里真正存储的类型.
请注意,此文件一定要放到与IHelloService.java相同的目录下面,否则会出错。
5、在Web.xml中配置XFire需要用到的Servlet,代码如下:
![](../../../Images/OutliningIndicators/None.gif)
2
![](../../../Images/OutliningIndicators/None.gif)
3
![](../../../Images/OutliningIndicators/None.gif)
4
![](../../../Images/OutliningIndicators/None.gif)
5
![](../../../Images/OutliningIndicators/None.gif)
6
![](../../../Images/OutliningIndicators/None.gif)
7
![](../../../Images/OutliningIndicators/None.gif)
8
![](../../../Images/OutliningIndicators/None.gif)
9
![](../../../Images/OutliningIndicators/None.gif)
10
![](../../../Images/OutliningIndicators/None.gif)
11
![](../../../Images/OutliningIndicators/None.gif)
12
![](../../../Images/OutliningIndicators/None.gif)
13
![](../../../Images/OutliningIndicators/None.gif)
14
![](../../../Images/OutliningIndicators/None.gif)
15
![](../../../Images/OutliningIndicators/None.gif)
16
![](../../../Images/OutliningIndicators/None.gif)
17
![](../../../Images/OutliningIndicators/None.gif)
18
![](../../../Images/OutliningIndicators/None.gif)
19
![](../../../Images/OutliningIndicators/None.gif)
20
![](../../../Images/OutliningIndicators/None.gif)
21
![](../../../Images/OutliningIndicators/None.gif)
22
![](../../../Images/OutliningIndicators/None.gif)
23
![](../../../Images/OutliningIndicators/None.gif)
24
![](../../../Images/OutliningIndicators/None.gif)
25
![](../../../Images/OutliningIndicators/None.gif)
26
![](../../../Images/OutliningIndicators/None.gif)
27
![](../../../Images/OutliningIndicators/None.gif)
28
![](../../../Images/OutliningIndicators/None.gif)
此时Web Service的服务端就开发完成了。
我们来看看客户端的代码吧,也很简单,如下:
![](../../../Images/OutliningIndicators/None.gif)
2
![](../../../Images/OutliningIndicators/None.gif)
3
![](../../../Images/OutliningIndicators/None.gif)
4
![](../../../Images/OutliningIndicators/None.gif)
5
![](../../../Images/OutliningIndicators/None.gif)
6
![](../../../Images/OutliningIndicators/None.gif)
7
![](../../../Images/OutliningIndicators/None.gif)
8
![](../../../Images/OutliningIndicators/None.gif)
9
![](../../../Images/OutliningIndicators/None.gif)
10
![](../../../Images/OutliningIndicators/None.gif)
11
![](../../../Images/OutliningIndicators/None.gif)
12
![](../../../Images/OutliningIndicators/ExpandedBlockStart.gif)
![](../../../Images/OutliningIndicators/ContractedBlock.gif)
![](../../../Images/dot.gif)
13
![](../../../Images/OutliningIndicators/InBlock.gif)
14
![](../../../Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](../../../Images/OutliningIndicators/ContractedSubBlock.gif)
![](../../../Images/dot.gif)
15
![](../../../Images/OutliningIndicators/InBlock.gif)
16
![](../../../Images/OutliningIndicators/InBlock.gif)
17
![](../../../Images/OutliningIndicators/InBlock.gif)
18
![](../../../Images/OutliningIndicators/InBlock.gif)
19
![](../../../Images/OutliningIndicators/InBlock.gif)
20
![](../../../Images/OutliningIndicators/InBlock.gif)
21
![](../../../Images/OutliningIndicators/InBlock.gif)
22
![](../../../Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](../../../Images/OutliningIndicators/ContractedSubBlock.gif)
![](../../../Images/dot.gif)
23
![](../../../Images/OutliningIndicators/InBlock.gif)
24
![](../../../Images/OutliningIndicators/InBlock.gif)
25
![](../../../Images/OutliningIndicators/InBlock.gif)
26
![](../../../Images/OutliningIndicators/InBlock.gif)
27
![](../../../Images/OutliningIndicators/InBlock.gif)
28
![](../../../Images/OutliningIndicators/InBlock.gif)
29
![](../../../Images/OutliningIndicators/InBlock.gif)
30
![](../../../Images/OutliningIndicators/InBlock.gif)
31
![](../../../Images/OutliningIndicators/InBlock.gif)