xfire集成spring开发webservice怎么配置发布地址
时间:2026-05-05 20:45:00
浏览:852次

1.首先建一个Web工程,引入相应的jar包,Xfire开发最精简jar包下载服务器端:commons-logging-1.1.1.jarjdom-1.0.jarorg.springframework.aop-3.1.1.RELEASE.jarorg.springframework.asm-3.1.1.RELEASE.jarorg.springframework.beans-3.1.1.RELEASE.jarorg.springframework.context-3.1.1.RELEASE.jarorg.springframework.core-3.1.1.RELEASE.jarorg.springframework.expression-3.1.1.RELEASE.jarorg.springframework.web.servlet-3.1.1.RELEASE.jarorg.springframework.web-3.1.1.RELEASE.jarwsdl4j-1.6.1.jarxfire-all-1.2.6.jar客户端:com.springsource.org.junit-4.7.0.jarcommons-codec-1.3.jarcommons-httpclient-3.0.jarcommons-logging-1.1.1.jarjdom-1.0.jarwsdl4j-1.6.1.jarxfire-all-1.2.6.jarXmlSchema-1.1.jar 2.修改web.xml,加入以下代码:[html] view plain copy contextConfigLocation classpath:applicationContext.xml org.springframework.web.context.ContextLoaderListener xfireServlet org.codehaus.xfire.spring.XFireSpringServlet xfireServlet /service/* 3.在classpath下加入Spring配置文件applicationContext.xml,加入以下代码:[html] view plain copy 4.定义WebService接口,添加相应注解:[java] view plain copy@WebService public interface IBookService { @WebMethod public Book getBook(); } 5.接口实现类,注解中serviceName定义发布的服务名,endpointInterface定义实现的接口:[java] view plain copy@Component @WebService(serviceName="BookService", endpointInterface = "my.webservice.IBookService") public class BookServiceImpl implements IBookService { @Override public Book getBook() { Book b = new Book(1, "Java核心思想", 100); System.out.println(">>>>>>Server: " + b); return b; } } 6.以上便是服务端的配置及实现,是不是很简单。把工程发布到Tomcat并启动,在浏览器中输入:http://127.0.0.1:8080/XFireTest/service/BookService?wsdl(BookService为上面serviceName定义的名称),如果浏览器中显示BookService相关xml信息,则表示WebService发布成功。 7.客户端调用服务端方法: (1)使用接口调用,此方法需要发布服务者提供接口,或我们自己通过wsdl生成接口。自己生成接口方法:wsimport -keep -p my.client http://127.0.0.1:8080/XFireTest/service/BookService?wsdl-keep 指示保留生成的文件,-p 指定需要在其中生成构件的包名称。http://127.0.0.1:8080/XFireTest/service/BookService?wsdl 是WSDL文件的位置。wsimport在JAVA_HOME/bin目录下,应该已加到path环境变量中,可直接在cmd中运行以上命令,注意先进入要生成类的目录(如src)再执行。[java] view plain copy@Test public void testBookService() { Service serviceModel = new ObjectServiceFactory().create(IBookService.class); String url = "http://127.0.0.1:8080/XFireTest/service/BookService"; IBookService service = null; try { service = (IBookService) new XFireProxyFactory().create(serviceModel, url); Book b = service.getBook(); System.out.println(">>>>>>>>Client: " + b); } catch (Exception e) { e.printStackTrace(); } } (2)通过wsdl调用,该方法如果返回值是String可正常使用,如果像本列中返回Book类型,则打印值为[#document: null],返回类型是org.apache.xerces.dom.DocumentImpl,需手动解析。我在项目中使用时,发布服务的时候把数据用xml形式返回,客户端用dom4j解析。[java] view plain copy@Test public void wsdlTest() throws MalformedURLException, Exception { Client client = new Client(new URL("http://127.0.0.1:8080/XFireTest/service/BookService?wsdl")); Object[] results = client.invoke("getBook", new Object[] {}); System.out.println(results[0]); }