ITeye博客

xfire1.2.6 srping2.5 集成WebService

kaobian · 2012-04-28 10:36 · java · WebService · xfire · 企业架构 · Java · Spring · WebService · Markdown

和大家分享一下我做的demo案例,xfire和spring2.5集成。 需要的jar文件 [code="java"] 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 [/code] 服务器端 接口类: [code="java"] package iteye.kaobian; public interface IHello { public String sayHello(String name); } [/code] 接口实现类: [code="java"] package iteye.kaobian; public class SayHello implements IHello { public String sayHello(String name) { System.out.println("hello == " + name); return "hello " + name; } } [/code] spring文件配置: [code="java"] [/code] web.xml文件配置: [code="java"] contextConfigLocation classpath*:applicationContext.xml org.springframework.web.context.ContextLoaderListener xfire org.codehaus.xfire.spring.XFireSpringServlet xfire /WebService/* index.jsp [/code] 客户端调用WebService有两种方式,静态和动态,静态又分了很多种,我只讲一个和spring集成的: 代码: [code="java"] 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")); } } [/code] 客户端spring文件: [code="java"] com.bonc.IHello http://localhost:8080/hesper/WebService/IHello?wsdl [/code] 动态调用的客户端: [code="java"] 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]); } } [/code]

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。