从网上找到好多xfire+spring的例子 尝试着按照例子做,可惜一直没有试验出来,索性从几个例子中抽取一部分 组合起来终于把例子调试通过,
所需要的jar包 自己上网查询一下 我不在累赘
好了不多说直接上代码
首先要用myeclipse创建一个webservice 项目 如下图
注意要选xfire这个选项
然后 在项目中 新建一个接口 和一个实现类
具体如下
package webservice;
public interface HelloWorld {
/** *//**
*对名字为name的人打招呼.
*@paramname名字
*@return返回打招呼的字符串
*/
String sayHelloWorld(String name);
}
package webservice;
/** *//**
*HelloWorld的实现类.
*/
public class HelloWorldImpl implements HelloWorld {
public String sayHelloWorld(String name) {
String helloWorld = "hello," + name;
return helloWorld;
}
}
接下来就是配置啦 请看下面的目录结构
applicationContex.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="HelloWorldBean" class="webservice.HelloWorldImpl"/>
</beans>
然后是 web.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>XFireService</display-name>
<!-- begin Spring配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/xfire-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener
</listener-class>
</listener>
<!-- end Spring配置 -->
<!-- begin XFire 配置 -->
<servlet>
<servlet-name>xfire</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xfire</servlet-name>
<url-pattern>*.ws</url-pattern>
</servlet-mapping>
<servlet>
<!-- 配合Spring容器中XFire一起工作的Servlet-->
<servlet-name>xfireServlet</servlet-name>
<servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xfireServlet</servlet-name>
<!-- 在这个URI下开放Web Service服务 -->
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
<!-- end XFire 配置 -->
</web-app>
在然后是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="/HelloWorldService.ws">
<ref bean="HelloWorldService" />
</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="HelloWorldService" parent="baseWebService">
<!-- 业务服务bean -->
<property name="serviceBean" ref="HelloWorldBean" />
<!-- 业务服务bean的窄接口类 -->
<property name="serviceClass" value="webservice.HelloWorld" />
</bean>
</beans>
好啦 代码写好 配置也完成了 接下来 就是新建一个测试类
代码如下:
package test;
import java.net.MalformedURLException;
import org.codehaus.xfire.XFire;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import webservice.HelloWorld;
public class WebServiceClientTest {
/* Call the Web service
*
*/
public String callWebService(String name) {
//Create a metadata of the service
Service serviceModel = new ObjectServiceFactory().create(HelloWorld.class);
//log.debug("callSoapServiceLocal(): got service model." );
//Create a proxy for the deployed service
XFire xfire = XFireFactory.newInstance().getXFire();
XFireProxyFactory factory = new XFireProxyFactory(xfire);
String serviceUrl = "http://localhost:9000/xfire/HelloWorldService.ws";
HelloWorld client = null;
try {
client = (HelloWorld) factory.create(serviceModel, serviceUrl);
} catch (MalformedURLException e) {
//log.error("WsClient.callWebService(): EXCEPTION: " + e.toString());
}
//Invoke the service
String serviceResponse = "";
try {
serviceResponse = client.sayHelloWorld(name);
} catch (Exception e){
//log.error("WsClient.callWebService(): EXCEPTION: " + e.toString());
serviceResponse = e.toString();
}
//log.debug("WsClient.callWebService(): status=" + serviceResponse);
//Return the response
return serviceResponse;
}
//测试住方法
public static void main(String[] args){
WebServiceClientTest wt = new WebServiceClientTest();
String s=wt.callWebService("你妹");
System.out.println(s);
}
}
注意我的tomcat 端口是9000哦