转自:https://www.cnblogs.com/fu-yun/p/4553685.html
包含:axis.jar,commons-discovery.jar,commons-logging-1.0.4.jar,jaxrpc.jar,wsdl4j.jar,mail.jar。
为什么会有mail.jar呢?俺也不太清楚,如果没有的话会报警告:
-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); } }
来源:https://www.cnblogs.com/sharpest/p/7856129.html