Web Service 技术提供的思想和好.不过,刚开始学习的时候,对客户端和服务端之间角色的相互转换还是比较不好让人把握.所以,刚开始学习的时候不好掌握.对各种配置有 点乱.所以在学习的时候要及时的总结和练习.这样在工作中才不至于手忙脚乱.Web Service 只要将服务对外公开的话就要提供wsdl文档,是自动生成的.比喻成先干活后签合同.呵呵.Web Service的目的是更加关注业务
1.xfire
1)创建web文件夹结构
2)添加类库到/web-inf/lib下(注意:xfire-all.jar)
3)写服务类
4)在WEB-INF/classes/NETA-INF/xfire/services.xml创建xfire配置文件
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>BookService</name>
<namespace>http://xfire.codehaus.org/BookService</namespace>
<serviceClass>..BookService</serviceClass>
<implementationClass>..BookServiceImpl</implementationClass>
</service>
</beans>
5)web.xml
<servlet-class>org.codehaus.xfire..XFireConfigurableServlet</..
<mapping>/servlet/XFireServlet/*
<mapping>/services/*
2.使用axis客户端测试:
Service s = new Service();
Call call = (Call)s.createCall();
Call.setTargetEnp..(“url”);
Call.setOperationName(“sayHello”);
Call.invoke(..);
3.简单xfire客户端访问:
Client c = new Client(new URL(“..?wsdl”))//本机文件e:xxx.wsdl?
c.invoke(“sayHello”,new Object[]{..});
4.使用 XFireProxyFactory客户端
ObjectServiceFactory serviceFactory = new ObjectServiceFactory();
Service serviceModel = serviceFactory.create(Echo.class);
XFireProxyFactory proxyFactory = new XFireProxyFactory();//创建代理
Echo echo = (Echo) proxyFactory.create(serviceModel, "http://localhost:8191/Echo");
System.out.println(echo.echo("Hello World"));
5.使用Xfire命令行生成客户 端
使用命令行生成客户端存根
..WsGen [该命令需要ant包支持]
-wsdl http://localhost:8080/xfire/services/Bookservice?wsdl//wsdl文档位置
-o src
-p cn.itcast.xfire.book
-overwrite true
New xxxClient().getXxxPort().xxx();
注解:
-wsdl:wsdl文件位置/url
-o:生成类的输出目录
-package:客户端代码要被放置的包名
-overwrite:覆盖上次生成的文件.没有该项的话,若存在上次生成的文件,会拒绝生成类.
6.xfire 的 XML 配置
1).写services.xml
2).更新web.xml
确保spring和xbean在classpath中.
Services.xml默认位于classpath:META-INF/xfire/services.xml
Web.xml
<servlet-class>
org.codehaus.xfire.transport.http.XFireConfigurableServlet
</servlet-class>
<init-param>
<param-name>config</param-name><!-—WEB-INF-?
<param-value>services.xml</param-value>
</init-param>
<url-pattern>/servlet/XFireServlet/*</url-pattern>
<url-pattern>/services/*</url-pattern>
7.Java对象的传递
1)Javabean中的集合属性
public class Customer{
private Set orders;
Set getOrders() { return orders; }
public void setOrders(Set orders) { this.orders = orders };
}
使用property元素代替method和parameter.
<mappings>
<mapping>
<property componentType="org.codehaus.xfire.Employee" />
</mapping>
</mappings>
可以用<property>元素替代<method>和parameter.
2)Map:
需要增加keyType.
Public map getMap();
<method name=“getMap”>
<return keyType=“java.lang.String” componentType=“java.lang.String” />
<parameter index=“0” >[没有type属性]
总结:
--如果采用jdk1.5,使用泛型,不需要影射.因为已经明确指定了类型.如果是1.4(不支持泛型).需要创建xml映射文件来指定方法和集合类型.
--数组不需要注册
--List在服务器传递,在客户端接受安对象数组接收,所以要用数组接收.
--List,Map是否带泛型,客户端接收都是数组形式.
--RemoteException远程异常
传智播客--Web Service之xfire和对象的传递
最新推荐文章于 2024-08-12 16:50:44 发布