---
title: "xfire1.2.6 srping2.5 集成WebService"
author: "kaobian"
date: "2012-04-28T10:36:05Z"
tags: ["Java", "Spring", "WebService"]
categories: ["企业架构"]
slug: "xfire1-2-6-srping2-5-webservice"
draft: false
lastmod: "2012-04-28T10:36:05Z"
id: 1502864
url: "https://kaobian.iteye.com/blog/1502864"
---

和大家分享一下我做的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]