eclipse下利用ant生成xfire webservice 客户端
2013-08-30 13:48
477 查看
前言:
最近刚开始接触webservice开发,因为刚接的这个项目刚好是开发webservice的,但是以前没接触过这方面,所以花了点时间学这方面的知识,都说百度老师很万能,所以在网上百度了许多关于xfire webservice生成客户端方面的知识,但是上面的都有一些问题,也没人帮我,自己也走了不少弯路,这篇文章有什么错误和不足之处,望他家踊跃指出(我是刚毕业的新手)
还有,本文章直接切入正题,那些什么介绍啊就不多说了,大家要了解更多相关的技术介绍,自己就动手问百度老师吧
。
一、配置ant
1、到Apache官网下载ant最新版本(我一向都是走新路线
)http://ant.apache.org/bindownload.cgi,并解压出来
2、.配置环境变量,“我的电脑”----->“属性”--------->“高级”------->“环境变量”
(1)Path:D:\Program Files\apache-ant-1.9.2\bin;,网上大多数大多数都说要配置ANT_HOME,但是其实直接在变量Path下把ant的解压目录的bin目录拷贝过来就可以了。
(2)打开cmd窗口,输入:ant -version ,如出现如下信息就配置ant完成
二、配置xfire,并建立web工程
1、到官网下载xfire,下载地址:http://xfire.codehaus.org/Download ,并解压。
2、打开eclipse新建一个Dynamic web Project (如下)
(1)建立上图的包和文件
(2)上图中的类信息如下:
User类:
package com.xfire.bean;
public class User {
private int id;
private String name;
public User(String name){
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Hello服务接口类:
package com.xfire.services;
import com.xfire.bean.User;
public interface Hello {
public static final int c = 1;
public String say();
public User eat();
}
HelloImplement接口实现类:
package com.xfire.services;
import com.xfire.bean.User;
public class HelloImplement implements Hello {
@Override
public String say() {
return "欢迎使用吉量软件。。。。。。。。";
}
@Override
public User eat() {
return new User("zhangsan");
}
}
(3)Services.xml文件和web.xml文件配置
Services.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<!-- 配置service -->
<service>
<name>HelloXfire</name> <!-- 服务名 -->
<namespace>http://localhost:8088/HelloXfire</namespace><!-- 命名空间,按理说可以按你的意愿去命名 -->
<serviceClass>com.xfire.services.Hello</serviceClass><!-- 服务类接口 -->
<implementationClass>com.xfire.services.HelloImplement</implementationClass><!-- 服务接口实现类 -->
</service>
<!--
如果你提供的服务不是太复杂,可以不定义接口,直接写实现类也是可以的,你们可以测试一下
<service>
<name>Myservices</name>
<namespace>www.cwl.com</namespace>
<serviceClass>com.xfire.services.MyService</serviceClass> 服务实现类
</service> -->
</beans>
web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Xfires</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置xfire -->
<servlet>
<servlet-name>Xfires</servlet-name>
<servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Xfires</servlet-name>
<url-pattern>/servlet/XFireServlet/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Xfires</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
注:有些哥们儿创建web工程的时候没有web.xml,是因为你新建工程把名字填好后就finish了,所以没有,建立web工程填好工程名的时候千万别急着finish,要next,直到出现下图的样式
把checkbox打上勾,然后finish就会有web.xml了
(3)导入xfire所有关联的包
i:到xfire官网去下载相关包,我下载的是xfire-distribution-1.2.6,这个是最新版本了,下载地 址:http://xfire.codehaus.org/Download
ii:解压xfire-distribution-1.2.6,并把xfire-all-1.2.6.jar包和lib目录下的所有包拷贝到Web工程下的lib包下
好了,基本上搭建好了,然后把项目发布到tomcat上,启动服务器,在浏览器内输入 http://localhost:8080/Xfire/services/HelloXfire?wsdl后,回车出现如下信息 注意:HelloXfire是你services.xml里面的name
这个是服务器发送过来的wsdl文件,当出现如上图时服务器端就大功告成了!
三、客户端代码的生成
1、新建一个JAVA工程,建立如下文件和包
上图中的Xfire类库 和 lib里面装的都是xfire的依赖包,本来我是想只用一个类库搞定的,但是如果只用一个在build.xml中就找不到xfire的依赖包,所以就用这个笨方法了,重复就重复吧。
2、build.xml和build.properties配置
build.xml 配置:
<?xml version="1.0" encoding="UTF-8"?>
<project name="XFire-WsGen" basedir="." default="wsgen">
<!-- basedir="."中的"."指的是相对与build.xml文件存放的目录,build.xml文件在哪个目录,点代表的就是哪个目录,比如:build.xml在目录
D:\Program Files\j2ee\XfiresClient\src 下,所以"."代表的就是此目录-->
<property file="build.properties">
</property>
<path id="mypath">
<fileset file="${lib.dir}/*.jar">
<include name="*.jar"/>
</fileset>
<!-- fileset 的功能是引用和指定xfire的所以包的路径-->
</path>
<target name="clean" description="Prepare for clean build">
<!--删除客户端-->
<delete dir="${basedir}/myclient"/>
<mkdir dir="${basedir}/myclient"/>
</target>
<taskdef classpathref="mypath" name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask"></taskdef>
<!-- classpathref 代表引用id等于"mypath"下的所以包,若不写,则找不到org.codehaus.xfire.gen.WsGenTask-->
<target name="wsgen" depends="clean">
<taskdef name='WsGenTask' classname="org.codehaus.xfire.gen.WsGenTask" classpathref="mypath"/>
<WsGenTask outputDirectory="${basedir}" wsdl="${wsdl.dir}" package="myclient" overwrite="true"/>
</target>
</project>
build.properties配置:
lib.dir=D:/xfire/XfireClient/lib
wsdl.dir=http://localhost:8080/Xfire/services/HelloXfire?wsdl
## lib.dir 是客户端工程下lib包的路径
3、利用ant生成客户端代码
1、"windows"----->"show view"----->ant
2、添加bulid.xml文件到ant工具中
3、运行build.xml文件
4、刷新工程XfireClient工程后就生成完毕啦
5、测试
客户端代码已经生成完成啦,我们现在就用一个类来测试一下吧!
(1) 在工程src下新建包com.xfire.client ,并创建Test类
Test类
package com.xfire.client;
import com.xfire.bean.User;
import myclient.HelloXfireClient;
import myclient.HelloXfirePortType;
public class Test {
public static void main(String[] args) {
HelloXfireClient hx = new HelloXfireClient(); //获取客户端类
HelloXfirePortType hf = hx.getHelloXfireHttpPort();//获取服务器端的所有接口方法,就是Hello类里面的所有方法
System.out.println(hf.say());//远程调用Hello类里面的say()方法
User u = hf.eat();//远程调用eat()方法,返回对象
System.out.println(u.getName().getValue());//获得对象的name属性
}
}
(2)右键---->run as---->java application
大功告成啦,不知道大家有没有明白,没明白的可以加我QQ:442570663 我们一起共同探讨,我这个只是标准的xfire+ant生成的客户端代码,有些童鞋可能是xfire 和spring集成的,关于spring集成方面的方法我过些日子再发布吧,祝大家学习愉快!
最近刚开始接触webservice开发,因为刚接的这个项目刚好是开发webservice的,但是以前没接触过这方面,所以花了点时间学这方面的知识,都说百度老师很万能,所以在网上百度了许多关于xfire webservice生成客户端方面的知识,但是上面的都有一些问题,也没人帮我,自己也走了不少弯路,这篇文章有什么错误和不足之处,望他家踊跃指出(我是刚毕业的新手)
还有,本文章直接切入正题,那些什么介绍啊就不多说了,大家要了解更多相关的技术介绍,自己就动手问百度老师吧
。
一、配置ant
1、到Apache官网下载ant最新版本(我一向都是走新路线
)http://ant.apache.org/bindownload.cgi,并解压出来
2、.配置环境变量,“我的电脑”----->“属性”--------->“高级”------->“环境变量”
(1)Path:D:\Program Files\apache-ant-1.9.2\bin;,网上大多数大多数都说要配置ANT_HOME,但是其实直接在变量Path下把ant的解压目录的bin目录拷贝过来就可以了。
(2)打开cmd窗口,输入:ant -version ,如出现如下信息就配置ant完成
二、配置xfire,并建立web工程
1、到官网下载xfire,下载地址:http://xfire.codehaus.org/Download ,并解压。
2、打开eclipse新建一个Dynamic web Project (如下)
(1)建立上图的包和文件
(2)上图中的类信息如下:
User类:
package com.xfire.bean;
public class User {
private int id;
private String name;
public User(String name){
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Hello服务接口类:
package com.xfire.services;
import com.xfire.bean.User;
public interface Hello {
public static final int c = 1;
public String say();
public User eat();
}
HelloImplement接口实现类:
package com.xfire.services;
import com.xfire.bean.User;
public class HelloImplement implements Hello {
@Override
public String say() {
return "欢迎使用吉量软件。。。。。。。。";
}
@Override
public User eat() {
return new User("zhangsan");
}
}
(3)Services.xml文件和web.xml文件配置
Services.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<!-- 配置service -->
<service>
<name>HelloXfire</name> <!-- 服务名 -->
<namespace>http://localhost:8088/HelloXfire</namespace><!-- 命名空间,按理说可以按你的意愿去命名 -->
<serviceClass>com.xfire.services.Hello</serviceClass><!-- 服务类接口 -->
<implementationClass>com.xfire.services.HelloImplement</implementationClass><!-- 服务接口实现类 -->
</service>
<!--
如果你提供的服务不是太复杂,可以不定义接口,直接写实现类也是可以的,你们可以测试一下
<service>
<name>Myservices</name>
<namespace>www.cwl.com</namespace>
<serviceClass>com.xfire.services.MyService</serviceClass> 服务实现类
</service> -->
</beans>
web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Xfires</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置xfire -->
<servlet>
<servlet-name>Xfires</servlet-name>
<servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Xfires</servlet-name>
<url-pattern>/servlet/XFireServlet/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Xfires</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
注:有些哥们儿创建web工程的时候没有web.xml,是因为你新建工程把名字填好后就finish了,所以没有,建立web工程填好工程名的时候千万别急着finish,要next,直到出现下图的样式
把checkbox打上勾,然后finish就会有web.xml了
(3)导入xfire所有关联的包
i:到xfire官网去下载相关包,我下载的是xfire-distribution-1.2.6,这个是最新版本了,下载地 址:http://xfire.codehaus.org/Download
ii:解压xfire-distribution-1.2.6,并把xfire-all-1.2.6.jar包和lib目录下的所有包拷贝到Web工程下的lib包下
好了,基本上搭建好了,然后把项目发布到tomcat上,启动服务器,在浏览器内输入 http://localhost:8080/Xfire/services/HelloXfire?wsdl后,回车出现如下信息 注意:HelloXfire是你services.xml里面的name
这个是服务器发送过来的wsdl文件,当出现如上图时服务器端就大功告成了!
三、客户端代码的生成
1、新建一个JAVA工程,建立如下文件和包
上图中的Xfire类库 和 lib里面装的都是xfire的依赖包,本来我是想只用一个类库搞定的,但是如果只用一个在build.xml中就找不到xfire的依赖包,所以就用这个笨方法了,重复就重复吧。
2、build.xml和build.properties配置
build.xml 配置:
<?xml version="1.0" encoding="UTF-8"?>
<project name="XFire-WsGen" basedir="." default="wsgen">
<!-- basedir="."中的"."指的是相对与build.xml文件存放的目录,build.xml文件在哪个目录,点代表的就是哪个目录,比如:build.xml在目录
D:\Program Files\j2ee\XfiresClient\src 下,所以"."代表的就是此目录-->
<property file="build.properties">
</property>
<path id="mypath">
<fileset file="${lib.dir}/*.jar">
<include name="*.jar"/>
</fileset>
<!-- fileset 的功能是引用和指定xfire的所以包的路径-->
</path>
<target name="clean" description="Prepare for clean build">
<!--删除客户端-->
<delete dir="${basedir}/myclient"/>
<mkdir dir="${basedir}/myclient"/>
</target>
<taskdef classpathref="mypath" name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask"></taskdef>
<!-- classpathref 代表引用id等于"mypath"下的所以包,若不写,则找不到org.codehaus.xfire.gen.WsGenTask-->
<target name="wsgen" depends="clean">
<taskdef name='WsGenTask' classname="org.codehaus.xfire.gen.WsGenTask" classpathref="mypath"/>
<WsGenTask outputDirectory="${basedir}" wsdl="${wsdl.dir}" package="myclient" overwrite="true"/>
</target>
</project>
build.properties配置:
lib.dir=D:/xfire/XfireClient/lib
wsdl.dir=http://localhost:8080/Xfire/services/HelloXfire?wsdl
## lib.dir 是客户端工程下lib包的路径
3、利用ant生成客户端代码
1、"windows"----->"show view"----->ant
2、添加bulid.xml文件到ant工具中
3、运行build.xml文件
4、刷新工程XfireClient工程后就生成完毕啦
5、测试
客户端代码已经生成完成啦,我们现在就用一个类来测试一下吧!
(1) 在工程src下新建包com.xfire.client ,并创建Test类
Test类
package com.xfire.client;
import com.xfire.bean.User;
import myclient.HelloXfireClient;
import myclient.HelloXfirePortType;
public class Test {
public static void main(String[] args) {
HelloXfireClient hx = new HelloXfireClient(); //获取客户端类
HelloXfirePortType hf = hx.getHelloXfireHttpPort();//获取服务器端的所有接口方法,就是Hello类里面的所有方法
System.out.println(hf.say());//远程调用Hello类里面的say()方法
User u = hf.eat();//远程调用eat()方法,返回对象
System.out.println(u.getName().getValue());//获得对象的name属性
}
}
(2)右键---->run as---->java application
大功告成啦,不知道大家有没有明白,没明白的可以加我QQ:442570663 我们一起共同探讨,我这个只是标准的xfire+ant生成的客户端代码,有些童鞋可能是xfire 和spring集成的,关于spring集成方面的方法我过些日子再发布吧,祝大家学习愉快!
相关文章推荐
- 利用ant自动生成XFire客户端代码。
- xfire生成webservice,Android利用ksoap2访问webservice 之 客户端搭建
- WebService学习——利用Eclipse生成JAX-WS WebService客户端
- 基于XFire框架利用ant构建WebService的客户端程序
- Ant+Xfire自动生成WebService客户端
- linux环境下,利用gsoap生成webservice客户端进行应用程序开发的小结
- 利用myeclipse生成webservice客户端的java代码
- [Webservice] Eclipse根据wsdl文件自动生成webservice的调用客户端
- 利用wsdl2java工具生成webservice的客户端代码
- 【Webservice】 Eclipse根据wsdl文件自动生成webservice的调用客户端
- 利用wsdl2java工具生成webservice的客户端代码,编码问题
- 利用wsdl文件生成webservice客户端代码
- 用XFire的Eclipse Plugin生成web服务的客户端
- 在Linux下,利用gSoap生成的WebService客户端进行开发时遇到的问题
- eclipse 生成webservice 客户端
- 用eclipse调用远程webservice生成客户端代码
- 用xfire的Eclipse Plugin生成web服务的客户端
- 用AXIS2发布WebService的方法 使用eclipse插件生成服务端和客户端
- 用xfire的Eclipse Plugin生成web服务的客户端
- 用xfire的Eclipse Plugin生成web服务的客户端