xfire1.2.6 srping2.5 集成WebService

2 篇文章 0 订阅
1 篇文章 0 订阅
和大家分享一下我做的demo案例,xfire和spring2.5集成。
需要的jar文件

xfire-all-1.2.6.jar
jdom-1.0.jar
wsdl4j-1.6.1.jar
commons-codec-1.3.jar
commons-httpclient-3.0.jar
stax-api-1.0.1.jar
spring.jar
commons-logging-1.0.4.jar
spring-webmvc.jar
XmlSchema-1.1.jar

服务器端 接口类:

package iteye.kaobian;

public interface IHello {
public String sayHello(String name);
}


接口实现类:

package iteye.kaobian;

public class SayHello implements IHello {
public String sayHello(String name) {
System.out.println("hello == " + name);
return "hello " + name;
}
}


spring文件配置:

<?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" />

<bean name="hello" class="iteye.kaobian.SayHello"/>

<bean
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/dataService.ws">
<ref bean="dataService" />
</entry>
</map>
</property>
</bean>

<!-- 使用XFire导出器 -->
<bean id="baseWebService"
class="org.codehaus.xfire.spring.remoting.XFireExporter"
lazy-init="false" abstract="true">
<property name="serviceFactory" ref="xfire.serviceFactory" />
<property name="xfire" ref="xfire" />
</bean>

<bean id="dataService" parent="baseWebService" >
<property name="serviceBean" ref="hello"/>
<property name="serviceClass" value="iteye.kaobian.IHello" />
</bean>

</beans>



web.xml文件配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<servlet>
<servlet-name>xfire</servlet-name>
<servlet-class>
org.codehaus.xfire.spring.XFireSpringServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xfire</servlet-name>
<url-pattern>/WebService/*</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>



客户端调用WebService有两种方式,静态和动态,静态又分了很多种,我只讲一个和spring集成的:
代码:

package test;

import iteye.kaobian.IHello;

import org.codehaus.xfire.client.Client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;


public class TestClient {

IHello helloWorld = null;

public static void main(String[] args) throws Exception {
TestClient t = new TestClient();
t.method2();
}

public void method1() throws Exception {
String wsdl = "http://localhost:8080/Xfire/WebService/IHello?wsdl"; // 对应的WSDL文件
Resource resource = new ClassPathResource(wsdl);
Client client = new Client(resource.getInputStream(), null); // 根据WSDL创建客户实例
Object[] objArray = new Object[1];
objArray[0] = " iteye.kaobian ";
Object[] results = client.invoke("sayHello", objArray);
System.out.println(" result: " + results[0]);
}

public void method2() throws Exception {

ApplicationContext ctx = new ClassPathXmlApplicationContext("client.xml");
IHello a = (IHello) ctx.getBean("testWebService");
System.out.println(a.sayHello("fiugang"));
}
}

客户端spring文件:

<?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="testWebService"
class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean">
<property name="serviceClass">
<value>com.bonc.IHello</value>
</property>
<property name="wsdlDocumentUrl">
<value>
http://localhost:8080/hesper/WebService/IHello?wsdl
</value>
</property>
</bean>
</beans>


动态调用的客户端:

package test;
import java.net.URL;

import org.codehaus.xfire.client.Client;


public class DongTai {

public static void main(String[] args) throws Exception {
Client client = new Client(new URL("http://localhost:8080/Xfire/WebService/IHello?wsdl"));
Object[] results = client.invoke("sayHello", new Object[] {"==========="});
System.out.println((String) results[0]);
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值