Java 编写 Web Service——使用axis && axis2
来源:互联网 发布:淘宝贷款利息好高 编辑:程序博客网 时间:2024/06/02 15:39
Java编写WebService——使用axis && axis2
(只会初略使用,所以然还不太清楚)
axis2是axis的升级版,先使用axis进行编写webService:
1、导入axis的包,
2、在src下面新增一个普通的class类
public class whinfo {
public String QxCartonInfo(String message){
System.out.println("Conncted :"+message);
return (“AAAAAAAAAAAA”);
}
public String UxCartonPanelStockIn(String message){
return new ConfirmXml().GetXMLSNInfo(“BBBBBBBBBB”);
}
}
3、在web项目的WEB-INF目录下面新增文件“server-config.wsdd”,具体类容如下
<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns="http://xml.a
<globalConfiguration>
<parameter name="sendMultiRefs" value="true"/>
<parameter name="disablePrettyXML" value="true"/>
<parameter name="dotNetSoapEncFix" value="true"/>
<parameter name="enableNamespacePrefixOptimization" value="false"/>
<parameter name="sendXMLDeclaration" value="true"/>
<parameter name="sendXsiTypes" value="true"/>
<parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/>
</globalConfiguration>
<handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper"/>
<service name="whmsinfostock.jws" provider="java:RPC">
<parameter name="className" value="com.webservice.whinfo"/> //设定webservice的class路径
<parameter name="scope" value="request"/>
<parameter name="allowedMethods" value="*"/>
<messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</service>
<transport name="http">
<requestFlow>
<handler type="URLMapper"/>
</requestFlow>
</transport>
</deployment>
4、在WEN-INF目录下的web.xml文件中加入以下代码,后续呼叫webservice的时候其实就是通过servlet进行调用的<servlet>
<description></description>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
看以看到如下讯息:whinfo.java中的两个方法名称都已经显示在了页面上,
点击“wsdl”,可以进入页面“http://localhost:8080/项目名称/services/whmsinfostock.jws?wsdl”,这个url路径就是客户端需要访问的webservice的路径
以上一个基于axis的webservice服务就完成了
下面为了测试,参考了网上的文章,随便找了一个基于axis2开发的客户端进行调用,是完全新建的一个项目
参考(http://hejiantx.blog.163.com/blog/static/6186734201251825052113/)
调用成功!!!
但是随后悲剧开始发生了,感觉另外新建一个项目太繁琐了,准备在原有的项目基础上增加一个jsp的测试页面,
编写的过程中,发现有axis包调用错误,应该是一个项目同时加载axis、axis2两种包,随即产生想法,全部使用axis2进行编写(详情见下篇)
package com.cm.client;
import javax.xml.namespace.QName;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
public class AxisUtil {
public static void main(String[] args) {
String xmlStr="xiaoxu.wang";
String url="http://localhost:8080/项目名称/services/whmsinfostock.jws?wsdl";
String method="QxCartonInfo";
AxisUtil.sendService(xmlStr,url,method);
}
public static String sendService(String xmlStr,String url,String method){
String xml=null;
try {
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference(url);
options.setTo(targetEPR);
// 在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,也就是<wsdl:definitions>元素的targetNamespace属性值
QName opAddEntry = new QName("http://service.cm.com",method);
// 参数,如果有多个,继续往后面增加即可,不用指定参数的名称
Object[] opAddEntryArgs = new Object[] {xmlStr};
// 返回参数类型,这个和axis1有点区别
// invokeBlocking方法有三个参数,其中第一个参数的类型是QName对象,表示要调用的方法名;
// 第二个参数表示要调用的WebService方法的参数值,参数类型为Object[];
// 第三个参数表示WebService方法的返回值类型的Class对象,参数类型为Class[]。
// 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}
// 如果被调用的WebService方法没有返回值,应使用RPCServiceClient类的invokeRobust方法,
// 该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同
Class[] classes = new Class[] { String.class };
xml=(String)serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0];
System.out.println(xml);
} catch (Exception e) {
e.printStackTrace();
long end = System.currentTimeMillis();
}
return xml;
}
}
- Java 编写 Web Service——使用axis && axis2
- Java 编写 Web Service——使用axis && axis2 (续)
- Axis Web Service开发之旅 (八) --编写Axis2模块(Module)
- Axis Web Service开发之旅 (八) --编写Axis2模块(Module)
- Linux底下在Eclipse中将Java工程生成Web Service接口(Axis和Axis2)
- 使用Axis开发Web Service
- 使用Axis开发Web Service
- 使用axis开发web service
- 使用Axis发布Web Service
- 使用Axis2调用Web Service
- 使用 Apache Axis2 和 Eclipse 创建 Java Web Service
- java axis发布web service(一)安装Axis
- 使用Axis开发Web Service程序
- 使用Axis开发Web Service程序
- 使用Axis开发Web Service程序
- 使用Axis开发Web Service程序
- 使用Axis开发Web Service程序
- 使用Axis开发Web Service程序
- 2分钟学会GCD
- [TOP10]十大渗透测试演练系统
- C语言编程积累2
- 主成分分析(Principal components analysis)-最小平方误差解释
- jquery动态添加删除行
- Java 编写 Web Service——使用axis && axis2
- Fork/Join框架小示例-计算连续任意大个数之和
- c语言编写经验逐步积累3
- 魔兽改建程序源码(一)
- 指针自身的地址问题
- C++必知必会(2)
- LeetCode Simplify Path
- 递归转非递归几个实例
- Find all max of elements of an array