1.下载支持XFire的Jar包.
如果使用MyEclipse的话则不用下载,因为MyEclipse已经集成.在工程上右键选择MyEclipse->add WebService...进行添加,此处不做介绍.
可以访问xfire.codehaus.org下载XFire框架的安装包,下载时请选择“全部二进制发布包(BinaryDistributioninzippackage)”,而不仅仅是“XFirejar文件(JarofallXFiremodules)”。下载完毕后解压,把根据目录下的xfire-all-1.2.6.jar和lib文件夹中的所有文件都拷贝到工程的WEB-INF/lib中.
2.配置XFire的运行环境.
修改工程中的WEB-INF/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">
- <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>
<?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"> <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>
注意:此处可以在原来Web.xml文件上进行修改,只需求添加<servlet>和<servlet-mapping>标签即可,引入了XFireServlet类,以处理Web Service请求,并且负责提供Web Service的WSDL(Web服务描述语言)
3.在WEB-INF/classes下创建文件夹:/META-INF/xfire,并在其中创建services.xml文件,文件中内容如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://xfire.codehaus.org/config/1.0">
- </beans>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://xfire.codehaus.org/config/1.0"> </beans>
4.此时WebService的环境已经搭建完毕.
在地址栏中输入http://localhost:8888/TestXFire/services,如果不报错说明配置成功.因为没有创建任何WebService所以显示如下信息:
Generated by XFire ( http://xfire.codehaus.org )
5.创建WebService.
ISayHelloService.java文件如下:
- package com.neusoft.study;
- public interface ISayHelloService
- {
- public String sayHello(String name) ;
- }
package com.neusoft.study;
public interface ISayHelloService
{
public String sayHello(String name) ;
}
这个接口告诉服务器,你的WebService哪些方法是可以被用户调用的.下面再来写一个ISayHelloService的实现类,以完成业务逻辑.
SayHelloServiceImpl.java文件如下:
- package com.neusoft.study;
- public class SayHelloServiceImpl implements ISayHelloService
- {
- public String sayHello(String name)
- {
- if(name==null || "".equals(name)){
- return "为什么不告诉我你的名字?" ;
- }
- return ",你妈喊你回家吃饭啦!";
- }
- public String noSay(String name){
- return name+",不告诉你" ;
- }
- }
package com.neusoft.study;
public class SayHelloServiceImpl implements ISayHelloService
{
public String sayHello(String name)
{
if(name==null || "".equals(name)){
return "为什么不告诉我你的名字?" ;
}
return ",你妈喊你回家吃饭啦!";
}
public String noSay(String name){
return name+",不告诉你" ;
}
}
这个类实现了sayHello方法,这个方法是可以通过WebService访问到的.另外一个方法noSay(),该方法没有在接口ISayHelloService中定义,所以不能被WebService调用到.
6.发布WebService.
修改WEB-INF/classes/META-INF/xfire/services.xml文件,修改后的文件如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://xfire.codehaus.org/config/1.0">
- <service>
- <name>SayHelloService</name>
- <serviceClass>com.neusoft.study.ISayHelloService</serviceClass>
- <implementationClass>com.neusoft.study.SayHelloServiceImpl</implementationClass>
- </service>
- </beans>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://xfire.codehaus.org/config/1.0"> <service> <name>SayHelloService</name> <serviceClass>com.neusoft.study.ISayHelloService</serviceClass> <implementationClass>com.neusoft.study.SayHelloServiceImpl</implementationClass> </service> </beans>
这个文件定义了一个WebService:SayHelloService,并且同时定义了接口和实现类
7.启动Tomcat。
启动Tomcat,在浏览器地址栏中输入http://localhost:8888/TestXFire/services,会显示如下信息:
Available Services:
- HelloWebService [wsdl]
Generated by XFire ( http://xfire.codehaus.org )
点击[wsdl],会显示相应的WSDL信息。
8.测试WebService。
编写一个具有main函数的类,用于测试WebService.
- package com.zhang.main;
- import java.net.MalformedURLException;
- import org.codehaus.xfire.client.Client;
- import org.codehaus.xfire.client.XFireProxyFactory;
- import org.codehaus.xfire.service.Service;
- import org.codehaus.xfire.service.binding.ObjectServiceFactory;
- import com.zhang.ISayHelloService;
- public class SayHello
- {
- /**
- * <p>Description:[方法功能描述]</p>
- * @param args
- * @author:{ysj}
- * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
- */
- public static void main(String[] args)
- {
- String serviceURL = "http://localhost:8888/TestXFire3/services/SayHelloService" ;//SayHelloService是在services.xml中配置的name标签的值
- Service serviceModel = new ObjectServiceFactory().create(ISayHelloService.class, null, "http://localhost:8888/TestXFire3/services/SayHelloService?wsdl", null) ;//此处的URL为WSDL所在URL
- XFireProxyFactory serviceFactory = new XFireProxyFactory() ;//通过代理工厂创建一个service工厂
- try
- {
- ISayHelloService service = (ISayHelloService)serviceFactory.create(serviceModel, serviceURL) ;
- Client client = Client.getInstance(service) ;
- String hello = service.sayHello(null) ;
- System.out.println("服务器对无名氏说:"+hello) ;
- hello = service.sayHello("张三") ;
- System.out.println("服务器对[张三]说:"+hello) ;
- }
- catch (MalformedURLException e)
- {
- e.printStackTrace();
- }
- }
- }
package com.neusoft.main;
import java.net.MalformedURLException;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import com.neusoft.study.ISayHelloService;
public class SayHello
{
/**
* <p>Description:[方法功能描述]</p>
* @param args
* @author:{ysj}
* @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
*/
public static void main(String[] args)
{
String serviceURL = "http://localhost:8888/TestXFire3/services/SayHelloService" ;//SayHelloService是在services.xml中配置的name标签的值
Service serviceModel = new ObjectServiceFactory().create(ISayHelloService.class, null, "http://localhost:8888/TestXFire3/services/SayHelloService?wsdl", null) ;//此处的URL为WSDL所在URL
XFireProxyFactory serviceFactory = new XFireProxyFactory() ;//通过代理工厂创建一个service工厂
try
{
ISayHelloService service = (ISayHelloService)serviceFactory.create(serviceModel, serviceURL) ;
Client client = Client.getInstance(service) ;
String hello = service.sayHello(null) ;
System.out.println("服务器对无名氏说:"+hello) ;
hello = service.sayHello("张三") ;
System.out.println("服务器对[张三]说:"+hello) ;
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
}
}
输出结果为:
服务器对无名氏说:为什么不告诉我你的名字?
服务器对[张三]说:,你妈喊你回家吃饭啦!
服务器对[张三]说:,你妈喊你回家吃饭啦!
转载于:https://blog.51cto.com/4247649/1140883