看了很多网上写的使用XFire创建webservice的文章和帖子,难免有些不全的,今天整理了一份比较全的,并把一些错误的解决方式罗列出来。
注明:此示例只显示一个调用webservice打印出结果的简单过程。首先创建了需要的两个工程,一个视为服务端,一个视为客户端。
webserviceDemo为webservice工程,service为普通java工程。
1.首先创建webservice工程,创建过程就不截图多写了,网上很多,记得选择XFire即可。
2.在创建好的webserviceDemo中的WEB-INFO下面新增两个XML文件:
1)applicationContext.xml;2)xfire-servlet.xml
在applicationContext.xml只需要定义bean即可,内容如下:applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="SayHelloBean" class="impl.SayHelloImpl"/>
</beans>
在xfire-servlet.xml中定义XFire导出器,配置xfire信息,内容如下:xfire-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 引入XFire预配置信息 -->
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
<!-- 定义访问的url -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/SayHelloService.ws">
<ref bean="SayHelloService" />
</entry>
</map>
</property>
</bean>
<!-- 使用XFire导出器 -->
<bean id="baseWebService" class="org.codehaus.xfire.spring.remoting.XFireExporter" lazy-init="false" abstract="true">
<!-- 引用xfire.xml中定义的工厂 -->
<property name="serviceFactory" ref="xfire.serviceFactory" />
<!-- 引用xfire.xml中的xfire实例 -->
<property name="xfire" ref="xfire" />
</bean>
<bean id="SayHelloService" parent="baseWebService">
<!-- 业务服务bean -->
<property name="serviceBean" ref="SayHelloBean" />
<!-- 业务服务bean的窄接口类 -->
<property name="serviceClass" value="webservice.SayHello" />
</bean>
</beans>
--------------------------------------------------
<entry key="/SayHelloService.ws">
<ref bean="SayHelloService" />
</entry>key值为要访问webservice路径名称,<ref bean>为指向业务bean名称,业务bean指向为application.xml中的bean
-------------------------------------------------
3.建好配置文件后,分别在src下面创建接口和接口实现类,本人创建的接口名:SayHello,接口实现类:SayHelloImpl。
4.接口中定义一个方法:SayHello.java package webservice;
public interface SayHello {
public String sayHello(String name);
}
5.实现类实现方法:SayHelloImpl.javapackage impl;
import webservice.SayHello;
public class SayHelloImpl implements SayHello {
public String sayHello(String name) {
String helloName= "hello," + name;
return helloName;
}
}
此时webservice工程已经建好,建好后的目录结构如下图:
6.把项目部署到tomcat中,本人用的myeclipse6.5,tomcat5.5.20,部署过程相信搞java的童鞋们都会吧,就不多讲了。7.启动tomcat,访问webservice路径,http://localhost:8080/webserviceDemo/SayHelloService.ws?wsdl 如果网页中出现XML文本,XML文件中可以看到接口类的一些内容,表示webservice创建成功,能够成功访问了。二.创建客户端调用webservice
之前在网上看到有人在webservice工程下创建客户端调用程序,试了试,没成功。会出现异常
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/httpclient/methods/RequestEntity
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.getConstructor(Unknown Source)
at org.codehaus.xfire.transport.http.HttpChannel.sendViaClient(HttpChannel.java:108)
at org.codehaus.xfire.transport.http.HttpChannel.send(HttpChannel.java:48)
at org.codehaus.xfire.handler.OutMessageSender.invoke(OutMessageSender.java:26)
at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131)
at org.codehaus.xfire.client.Invocation.invoke(Invocation.java:79)
at org.codehaus.xfire.client.Invocation.invoke(Invocation.java:114)
at org.codehaus.xfire.client.Client.invoke(Client.java:336)
at org.codehaus.xfire.client.Client.invoke(Client.java:368)
at impl.SayTest.sayClient(SayTest.java:30)
at impl.SayTest.main(SayTest.java:12)
说是要引入commons-httpclient包,但引入后还是报异常。
然后我又重建了一个普通java工程,只用作客户端调用。
1.创建普通java工程,右键工程,bulid path ----add libraries---myeclipse libraries 选择XFIRE.12CORE 和XFIRE 1.2HTTP client
2.创建调试java类,函main函数:SayHelloTest.java
package com.test;
import java.net.URL;
import org.codehaus.xfire.client.Client;
public class SayHelloTest {
public static void main(String[] args) throws Exception {
SayHelloTest say = new SayHelloTest();
say.sayClient();
}
/**
*
* Context:
* 方法说明:
* @throws Exception
* @anthour */
public void sayClient() throws Exception {
//根据WSDL创建客户实例
Client client = new Client(new URL(
"http://localhost:8080/WebServiceDemo/SayHelloService.ws?wsdl"));
Object[] objArray = new Object[1];
objArray[0] = "张三";
//调用特定的Web Service方法
Object[] results = client.invoke("sayHello", objArray);
System.out.println("=========" + results[0]+"===========");
}
}
测试,输出:hello,张三。(成功。)