java webservice 调用

1 需要必备的开发环境:tomcat+eclipse+jdk

2 网上下载xfire-distribution-1.2.6.zip包,将里边的jar拷贝到工程web的lib下

3 在ide里边新建一个web service工程,会自动生成一个services.xml文件,文件中将配置上信息

4 service代码 

4.1创建一个java接口类:

package com.taikang.ws;

public interface IHelloService
{
	public String IHelloService(String name);
}

4.2创建实现类:

package com.taikang.ws;

public class HelloServeceImpl implements IHelloService
{
	public String IHelloService(String name)
	{
		// TODO Auto-generated method stub
		String ret = "进入服务器,已经获得信息,name:" + name + ".";
		return ret;
	}
}
4.3配置service.xml文件内容
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">

<!-- 配置webservice调用的名字 -->
	<service>
		<name>IHelloService</name>
		<serviceClass>com.taikang.ws.IHelloService</serviceClass>
		<implementationClass>com.taikang.ws.HelloServeceImpl</implementationClass>
	</service>

</beans>
name:(IHelloService)提供客户端调用接口的名字
serviceClass:实现类
implementionClass:接口
 
配置完,重启服务器,输入网址:
http://10.136.16.161:9080/webservice/services/IHelloService?wsdl


10.136.16.161:本机ip
9080 服务器端口
webservice:服务器中发布的工程名
services:services.xml文件
IHelloService:调用的接口名字

 

在服务器启动情况下,页面有返回,则services布置成功

 
5 测试代码
新建一个web工程,新建类函数:
public String send(String name)
	{ 
		String portType = "IHelloService";
		String feResponseXml ="";
		String webLogUrl = "http://10.136.16.161:9080/webservice/services/IHelloService?wsdl";
		String timeout = null;
		try
		{
			if(timeout==null)
				timeout = "60000";
			System.setProperty("sun.net.client.defaultConnectTimeout", timeout); 
			System.setProperty("sun.net.client.defaultReadTimeout", timeout);
			URL url = new URL(webLogUrl);
			Client client = new Client(url);
			client.setTimeout(Integer.parseInt(timeout));
			client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, timeout);
			
			Object[] result = client.invoke(portType, new Object[] {name});
			if(result !=null && result.length>0 && result[0]!=null)
			{
				feResponseXml = result[0].toString();
			}
			System.out.println("出参:"+feResponseXml);
		} 
		catch (Exception e)
		{
			e.printStackTrace();
			System.out.println(" send error:" + e);
		}
		return feResponseXml;
	}

调用功能类:

package com.taikang.ws;

public class WSClient
{
	public static void main(String[] args)
	{
		System.out.println("start...");
		Clientdeal wsc = new Clientdeal();
		String retmsg = wsc.send("wang");
		System.out.println("retMsg:" + retmsg);
	}
}
 
注:如果调用出现错误:找不到服务器提供的方法,原因可能是服务器提供的类方法与service.xml中配置不一样的问题。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值