标签:c style class blog code java
互操作性:实现不同系统间的相互调用(语言无关、平台无关)
<servlet> <display-name>XFireServlet</display-name> <servlet-name>XFireServlet</servlet-name> <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>services.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>XFireServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
新建接口和实现类:
新建接口:
cn.edu.hqu.service下SumService;
package cn.edu.hqu.service; public interface SumService { /** * 返回1-n的累加和 * @param n * @return */ public int getSum(int n); }
package cn.edu.hqu.service.impl; import com.service.SumService; public class SumServiceImpl implements SumService { public int getSum(int n) { int sum = 0; for (int i = 1; i <= n; i++) { sum += i; } return sum; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://xfire.codehaus.org/config/1.0"> <service> <name>SumService</name> <serviceClass>cn.edu.hqu.service.SumService</serviceClass> <implementationClass>cn.edu.hqu.service.impl.SumServiceImpl</implementationClass> <style>wrapped</style> <use>literal</use> <scope>application</scope> </service> </beans>
public static void main(String[] args) throws Exception { //加载xfire //创建服务 Service service = new ObjectServiceFactory().create(SumService.class); //WSDL地址 String url = "http://localhost:8080/SumWebService/services/SumService"; XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire()); SumService sumService = (SumService) factory.create(service, url); //保证服务端有开启 System.out.println(sumService.getSum(100)); }运行之前,webService服务端(SumWebService)要先部署启动;
public static void main(String[] args) throws Exception { String wsdlLocation = "http://localhost:8080/SumWebService/services/SumService?wsdl"; Client client = new Client(new URL(wsdlLocation)); Object[] o =client.invoke("getSum",new Object[]{100}); System.out.println(o[0]); }运行main输出结果;
private String getConfigPath() { if (configPath == null || configPath.length() == 0) { return CONFIG_FILE; } return configPath; }
一开始会得到这个路径,如果路径为空的话,就会使用默认的路径;
所以我们在wen.xml里买指定 configPath路径,src下直接写文件名
<init-param> <param-name>config</param-name> <param-value>services.xml</param-value> </init-param>这样就不会报错了,或者可以将services.xml放在新建的META-INF/xfire/services.xml下;
上课笔记_Web服务,XFire的一个例子,布布扣,bubuko.com
标签:c style class blog code java
原文地址:http://blog.csdn.net/jerome_s/article/details/28588415