eclipse下如何建Web Services以及在项目中的调用方法 (例子)
来源:互联网 发布:黑客帝国动画版矩阵化 编辑:程序博客网 时间:2024/06/01 20:44
1 建web services:
在eclipse下建一个java project,class CalculateService,代码如下,
public class CalculateService {
public float plus(float x, float y){
return x+y;
}
public float minus(float x, float y){
return x-y;
}
public float multiply(float x, float y){
return x*y;
}
public float divide(float x, float y){
return x/y;
}
}
选中项目,new-->Web Services。。。(具体内容可以参考网上资料,用的是axis2的创建方式)
创建好了之后,启动tomcat,你会发现有一条链接:http://localhost:8080/WebServiceProject/services/CalculateService?wsdl
这个链接就是你Web Services的地址。
2 调用Web Services(这是最简单的例子,大家可以根据具体的需求去编写)
在部署的另一个项目里,创建class Run,需要的jar文件:axis 和 axis2(需要的可以留邮箱问我要,也可以百度搜索下载)
以下代码的功能是 调用Web Services里的plus方法,输出结果3.0
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.soap.SOAP12Constants;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
public class Run {
EndpointReference targetEPR = new EndpointReference(
"http://localhost:8080/WebServiceProject/services/CalculateService?wsdl");
public void getWeb() {
try {
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace(
"http://ws.apache.org/axis2", "getData");// 命名空间
// OMElement method = fac.createOMElement("methodname", omNs);//
// 方法名称
// 请求参数设置
Options options = new Options();
options.setTo(targetEPR);// 设定webservice地址
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);// 设定传输协议
options.setSoapVersionURI(SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);// 设定SOAP版本soap1.2
// 客户端绑定参数设置
ServiceClient sender = new ServiceClient();
sender.setOptions(options);
// 设定访问的接口方法
OMElement method2 = fac.createOMElement("plus", omNs);// 要调用的接口方法名称
OMElement value1 = fac.createOMElement("x", omNs);// 方法的第一个参数名称
value1.addChild(fac.createOMText(value1, "1"));// 设定参数的值
OMElement value2 = fac.createOMElement("y", omNs);// 方法的第二个参数名称
value2.addChild(fac.createOMText(value2, "2"));// 设定参数的值
method2.addChild(value1);// 方法设置参数
method2.addChild(value2);// 方法设置参数
// 设定其他方法参数,针对参数是数组的情况如何处理?可以考虑为参数添加child,
// ............
OMElement result = sender.sendReceive(method2);// 调用接口方法
System.out.println(result);// 打印接口返回结果
Pattern pattern1 = Pattern.compile("<.+?>", Pattern.DOTALL);
Matcher matcher1 = pattern1.matcher(result.toString());
String string = matcher1.replaceAll("");
System.out.println(string);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Run r = new Run();
r.getWeb();
}
}
- eclipse下如何建Web Services以及在项目中的调用方法 (例子)
- Eclipse中的Web项目自动部署到Tomcat以及如何在Eclipse中使用My Eclipse工程
- 如何在ASP环境下利用SOAP来调用C#开发的Web Services
- 如何在ASP环境下利用SOAP来调用C#开发的Web Services
- 如何在ASP环境下利用SOAP来调用C#开发的Web Services
- 如何在eclipse中建web项目
- 在VC++6.0中调用Web Services的方法
- Web Services开发体会和在项目中的教训
- Web Services开发体会和在项目中的教训
- AJAX 调用Web Services 的简单例子
- 如何在eclipse上创建web项目(tomcat)
- 如何将eclipse中的web项目发布在tomcat中的webapps里
- 如何使用Soap消息调用Web Services
- 使用HTTP GET调用Web Services方法
- VC++调用天气预报Web services方法
- android项目在eclipse下调用第三方.so库的方法
- 如何在Eclipse下查看JDK源代码以及java源代码阅读方法
- 如何在Eclipse下查看JDK源代码以及java源代码阅读方法
- 让IT工作者过度劳累的13个坏习惯(转载)
- OCP考题解析_007: 同义词
- PacketFence安装
- jquery parent() parents() closest()区别
- python 发送 iOS APNS push 消息
- eclipse下如何建Web Services以及在项目中的调用方法 (例子)
- Qt采用Qextserialport 接收发送16进制数据
- 快速排序Python实现
- ubuntu NFS环境的搭建
- 软件测试过程中如何区分什么是功能bug,什么是需求bug,什么是设计bug?
- seo_robots.txt
- 拿着爱疯去乞讨?
- java实用的一些东西
- 导航栏: UINavigationBar用法