axis2 webservice 接口请求(json对象组装参数请求(用于对象请求)、多个参数请求、阿里巴巴json工具使用)
来源:互联网 发布:21天学通java第7版下载 编辑:程序博客网 时间:2024/05/10 16:35
这是一种啊啊
原地址
axis2 就不说太多了,我也不太懂,在apache上自己下载axis的jar包,导入到项目中,
下面就是实现代码了:
import java.util.Iterator;import javax.xml.namespace.QName;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.axis2.AxisFault;import org.apache.axis2.addressing.EndpointReference;import org.apache.axis2.client.Options;import org.apache.axis2.client.ServiceClient;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;public class Demo {private static void axis2WebService() { try { String soapBindingAddress = "http://192.168.0.55:9080/itsm/schemas/ProductServices?wsdl"; ServiceClient sender = new ServiceClient(); EndpointReference endpointReference = new EndpointReference(soapBindingAddress); Options options = new Options(); options.setTo(endpointReference); sender.setOptions(options); OMFactory fac = OMAbstractFactory.getOMFactory(); // 这个和qname差不多,设置命名空间 OMNamespace omNs = fac.createOMNamespace("http://www.chinawiserv.com/onecenter",""); //这个是namespace的strOMElement data = fac.createOMElement("getProducts", omNs); //getProducts是方法// 对应参数的节点 String[] strs = new String[] { "arg0" }; // 参数值 ,以json的格式进行传递String[] val = new String[] { "{\"userId\":\"1\"}"};for (int i = 0; i < strs.length; i++) {QName qname=new QName(strs[i]);OMElement inner = fac.createOMElement(qname);inner.setText(val[i]);data.addChild(inner);}System.out.println(data);// 发送数据,返回结果 OMElement result = sender.sendReceive(data);System.out.println(result.toString());//下面是返回的数据解析,返回的是json格式的数据,对string进行jsonobjectIterator iterator = result.getChildElements();OMElement result1 = null;while (iterator.hasNext()) { result1 = (OMElement) iterator.next();System.out.println(result1.getText()); }String re = result1.getText();JSONObject json_test = JSON.parseObject(re);System.out.println(json_test.getString("info"));System.out.println(json_test.getString("result"));} catch (AxisFault ex) {ex.printStackTrace();} } public static void main(String[] args) {axis2WebService();}}
第二种啊
原地址
免费下载地址在 http://linux.linuxidc.com/
用户名与密码都是www.linuxidc.com
具体下载目录在 /2015年资料/6月/6日/Java通过axis调用WebService/
下载方法见 http://www.linuxidc.com/Linux/2013-07/87684.htm
包含:axis.jar,commons-discovery.jar,commons-logging-1.0.4.jar,jaxrpc.jar,wsdl4j.jar,mail.jar。
为什么会有mail.jar呢?俺也不太清楚,如果没有的话会报警告:
2014-10-22 15:07:45 org.apache.axis.utils.JavaUtils isAttachmentSupported
警告: Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
网上有很多说这个问题的,但是还是没闹明白,还是加上mail.jar稳妥点。
对接webservice一定要有接口地址了
例如:
http://10.10.10.10:8080/xxxxx/services/XXXXWebservice?wsdl
直接上我写的测试代码吧(包名、地址、类名我都换成xxx了)
soapaction是通过访问接口地址获得的,targetNamespace
<wsdl:definitions targetNamespace="http://webservice.xxxx.com">package xx.xxxx.xxx;import java.net.URL;import javax.xml.namespace.QName;import javax.xml.rpc.ParameterMode;import javax.xml.rpc.encoding.XMLType;import org.apache.axis.client.Call;import org.apache.axis.client.Service;public class XXXWebService { public static void main(String[] args) { String soapaction = "http://webservice.xxxxxx.com"; //你的webservice地址 String endpoint = "http://10.10.10.10:8080/xxxxx/services/XXXXWebservice"; Service service = new Service(); try { Call call = (Call) service.createCall(); call.setTimeout(new Integer(60000)); call.setTargetEndpointAddress(new URL(endpoint)); //你需要远程调用的方法 call.setOperationName(new QName(soapaction,"getXXXX")); //方法参数,如果没有参数请无视 call.addParameter(new QName(soapaction,"xxxxxx"), XMLType.XSD_STRING, ParameterMode.IN); call.addParameter(new QName(soapaction,"xxxx"), XMLType.XSD_STRING, ParameterMode.IN); //设置返回类型,对方接口返回的json,我就用string接收了,自定义类型另贴一个代码 call.setReturnType(XMLType.XSD_STRING); //调用方法并传递参数,没有参数的话: call.invoke(new Object[] { null}); String result = (String) call.invoke(new Object[]{"xxxxx","xx,xx"}); System.out.println(result); } catch (Exception e) { e.printStackTrace(); } }}
下面是处理自定义类型 处理自定义返回类型我用的是dom4j jar包: http://pan.baidu.com/s/1jGGfNZg
call.setReturnType(XMLType.XSD_SCHEMA);
//没有参数就写个null
Schema data = (Schema) call.invoke(new Object[] { null});
MessageElement[] datas = data.get_any();
for (int i = 0; i < datas.length; i++) {
SAXReader reader = new org.dom4j.io.SAXReader();
Document doc = reader.read(new ByteArrayInputStream(datas[i].toString().getBytes()));
例:这是你调用方法返回的文档
<ns:getXXXXXXX>
<ns:return type="xx.xxx.xxx.xxxx.XXXXX">
<ax21:objects type="xx.xxx.xxx.xxxx.XXXXX">
<ax21:attribute>巴拉巴拉</ax21:attribute>
</ax21:objects>
</ns:return>
</ns:getXXXXXXX>
//这里的ns1:xxx 对应上面的ax21:objects
Node node = doc.selectSingleNode(“ns1:xxx”);
if (node != null) {
//这里就是获取object下的属性了
Node attribute= node.selectSingleNode(“ns1:attribute”);
System.out.println(attribute);
}
}
以上就是通过axis调用webservice的全部内容了
- axis2 webservice 接口请求(json对象组装参数请求(用于对象请求)、多个参数请求、阿里巴巴json工具使用)
- Jersery Restful接口服务接收JSON对象的参数请求
- IOS接口请求传递JSON参数
- IOS接口请求传递JSON参数
- ajax跨域请求springmvc传递json对象参数
- 从request对象中获取请求json格式的参数
- xutil3网络请求参数使用json结构
- Jmeter请求参数Json格式
- 异步请求、处理JSON对象
- jersey webService客户端发送put请求并传递json参数
- httpServletRequest对象获取请求参数
- 封装请求参数Page对象
- post请求公共接口,参数为json串
- JAVA HTTP POST请求(请求参数格式为json)
- http请求返回json对象。简单的接口处理
- restful接口实现跨域访问,以及get请求对象参数对象封装,post请求
- ios 网络请求参数为json
- python http请求post参数,json解析
- memcmp的问题
- Spring Boot访问mysql(JPA方式)最简单配置
- python 状态机语句"Python is fun"褒贬义判断的两种实现方式
- Unity NGUI UITexture 图片替换
- Atlantis HDU
- axis2 webservice 接口请求(json对象组装参数请求(用于对象请求)、多个参数请求、阿里巴巴json工具使用)
- SourceTree冲突解决
- 根据城市名获取天气
- Effective Java中文第四章第13节(个人渣翻)
- sublimeText3如何配置以支持jsx语法
- 机器学习随记1-逻辑回归(Logistic Regression)-1
- Python读写CSV文件
- IE的兼容问题
- I2C子系统之 adapter driver注册——I2C_dev_init()