太阳不下山 发表于 2021-7-29 21:39:36

java使用xfire创建和调用webservices

1.创建工程

File->New->Web Service Project,弹出Web Service Project窗口,需要填写Project Name(例子是Demo),选择XFire,然后一路next,直到完成。

创建完成后,打开生成的web.xml文件,可以看到,XFire已经配置好了。
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><display-name /><servlet>    <servlet-name>XFireServlet</servlet-name>    <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>    <load-on-startup>0</load-on-startup></servlet><servlet-mapping>    <servlet-name>XFireServlet</servlet-name>    <url-pattern>/services/*</url-pattern></servlet-mapping><welcome-file-list>    <welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>

2.创建WebService服务

选择Toolbars上的New Web Service,弹出New Web Service窗口,选择Strategy:Create web service from Java class(Bottom-up scenario)并勾选下面的Create new Java bean,然后Next>,在Web service name中填写MyService,在Java package栏点击New...弹出窗口中Name:中填com.demo.service,然后点Finish。

完成后,生成了一个Service的配置services.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://xfire.codehaus.org/config/1.0"><service><name>mysertest</name><serviceClass>com.mytest.services.Imysertest</serviceClass><implementationClass>com.mytest.services.mysertestImpl</implementationClass><style>wrapped</style><use>literal</use><scope>application</scope></service></beans>

生成了接口和默认实现:
package com.mytest.services;//Generated by MyEclipsepublic interface Imysertest {public String example(String message);}


package com.mytest.services;//Generated by MyEclipsepublic class mysertestImpl implements Imysertest {public String example(String message) {return message;}}

服务端代码生成完毕。
测试服务端:1.前提:配置Tomcat服务器,并完成WebService服务端的部署,然后启动Tomcat。

2.选择Toolbars上的Launch SOAP Web Service Explorer,Web Services Explorer窗口右侧WSDL Page,输入网址:http://localhost:8080/Demo/services/MyService?

客户端(调用服务端提供的WebService接口方法):

使用工具:eclipse

需要引入如下包:
commons-codec-1.2.jarcommons-httpclient-3.0.-rc2.jarjdom.jar、xfire-all-1.2.6.jarwsdl4j-1.5.1.jarcommons-logging-1.0.4.jar

开发步骤:1.创建工程

File->New->Java Project->Project name:Demo,一路Next>,最后Finish,然后新建包com.demo.client,包中建立2个文件,一个是服务端接口文件(直接复制粘贴过来)Imysertest.java,一个是测试文件Test.java,其代码如下:
package com.test.services;import org.codehaus.xfire.client.XFireProxyFactory;import org.codehaus.xfire.service.Service; import org.codehaus.xfire.service.binding.ObjectServiceFactory;public class servicesClient {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubString serviceUrl = "http://localhost:8080/mywebservices/services/mysertest";Service serviceModel = new ObjectServiceFactory().create(Imysertest.class, null, "http://localhost:8080/Demo/services/MyService?wsdl", null);XFireProxyFactory serviceFactory = new XFireProxyFactory();try{             Imysertest service = (Imysertest)serviceFactory.create(serviceModel,serviceUrl);String hello = service.example("hello");System.out.println(hello);         }catch(Exception e){             e.printStackTrace();         }}}

用到的jar


文档来源:51CTO技术博客https://blog.51cto.com/kymdidicom/3139429
页: [1]
查看完整版本: java使用xfire创建和调用webservices