`
mzh_2008beijing
  • 浏览: 230612 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Web Service的三种调用方式

阅读更多
    下面列举三种调用WEB SERVICE的方式,仅供参考:

方式一:spring + XFireClientFactoryBean的方式
  首先,配置XFireClientFactoryBean:
<bean id="helloService" class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean">
		<!-- 服务接口类 -->
		<property name="serviceClass">
			<value>com.mzh.server.IHelloService</value>
		</property>
		<!-- wsdlDocumentUrl -->
		<property name="wsdlDocumentUrl">
          <value>http://192.168.9.97:8089/AlarmCollector/services/HelloService?wsdl</value>
		</property>
	</bean>

其次,调用代码如下:
/**
	 * xfire+spring调用
	 */
	public static void invoke1(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		IHelloService helloService=(IHelloService)ctx.getBean("helloService");
		String helloMsg = helloService.sayHello("mazh");
		System.out.println(helloMsg);
	}

方式二:利用org.codehaus.xfire.client.Client实现调用
/**
	 * org.codehaus.xfire.client.Client客户端调用
	 */
	public static void invoke2(){
		try {
			Client client = new Client(new URL("http://192.168.9.97:8089/AlarmCollector/services/HelloService?wsdl"));
			Object[] results = client.invoke("sayHello", new Object[]{"zyd"});
			System.out.println(results[0]);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

方式三:利用axis.client实现调用
/**
	 * axis客户端调用
	 * @throws ServiceException
	 * @throws MalformedURLException
	 * @throws RemoteException
	 */
	public static void invovke3() throws ServiceException, MalformedURLException, RemoteException {
		String endpoint = "http://localhost:8089/AlarmCollector/services/HelloService";  
        Service service = new Service();  
        Call call = (Call) service.createCall();  
        call.setTargetEndpointAddress(new java.net.URL(endpoint));  
        call.setOperationName("sayHello");  
        call.addParameter("name",  
                org.apache.axis.encoding.XMLType.XSD_STRING, ParameterMode.IN);  
        call.setReturnType(XMLType.SOAP_STRING);  
        System.out.println(call.invoke(new Object[]{"mzh"}));  
	}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics