我是靠谱客的博主 无限汽车,最近开发中收集的这篇文章主要介绍java调用webservice WSDL形式接口之apache axis方式调用(一),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1. 先说一下什么是webservice

webservice的 发布一般都是使用WSDL(web service descriptive language)文件的样式来发布的,在WSDL文件里面,包含这个webservice暴露在外面可供使用的接口。

(1)WebService是一个SOA(面向服务的编程)的架构,它是不依赖于语言,不依赖于平台,可以实现不同的语言间的相互调用,通过Internet进行基于Http协议的网络应用间的交互。
(2)WebService实现不同语言间的调用,是依托于一个标准,webservice是需要遵守WSDL(web服务定义语言)/SOAP(简单请求协议)规范的。


2. 下面是介绍如何使用axis调用webservice

(1)先从官网下载axis相关jar包,地址:http://axis.apache.org/axis/

官方要求引入如下jar包(将lib下的jar引入即可)

  • axis-<version>/lib/axis.jar
  • axis-<version>/lib/jaxrpc.jar
  • axis-<version>/lib/saaj.jar
  • axis-<version>/lib/commons-logging-<version>.jar
  • axis-<version>/lib/commons-discovery-<version>.jar
  • axis-<version>/lib/wsdl4j-<version>.jar
我在项目中实际用到的最少jar如下:
H:libsaxis.jar
H:libsjaxrpc.jar
H:libscommons-logging-1.0.4.jar
H:libscommons-discovery-0.2.jar
H:libswsdl4j-1.5.1.jar

(2)具体代码实现
package ic;

import javax.xml.rpc.ParameterMode;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;

public class ICServiceTest1 {

	public static void main(String[] args) {

		try {

			String endpoint = "http://localhost:3000/hy/gw/queryConsignOrder?wsdl";

			Service service = new Service();
			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(endpoint);
			// WSDL里面描述的接口名称(要调用的方法)
			call.setOperationName("getLisQueryConsignOrder");
			// 接口方法的参数名, 参数类型,参数模式  IN(输入), OUT(输出) or INOUT(输入输出)
			call.addParameter("icSystem", XMLType.XSD_STRING, ParameterMode.IN);
			call.addParameter("companyFlag", XMLType.XSD_STRING, ParameterMode.IN);
			call.addParameter("consignOrderNo", XMLType.XSD_STRING, ParameterMode.IN);
			call.addParameter("orderNo", XMLType.XSD_STRING, ParameterMode.IN);
			// 设置被调用方法的返回值类型
			call.setReturnType(XMLType.XSD_STRING);
			//设置方法中参数的值
			Object[] paramValues = new Object[] {"4PL","NJHY,NJHF","TC201309172206",""};
			// 给方法传递参数,并且调用方法
			String result = (String) call.invoke(paramValues);	
	
			System.out.println("result is " + result);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}

===========================================异常解决=============================================================

今天在调用webserivce接口的时候出现了如下异常

警告: Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
AxisFault
 faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server
 faultSubcode: 
 faultString: org/apache/axis/client/Service
 faultActor: 
 faultNode: 
 faultDetail: 
	{http://xml.apache.org/axis/}stackTrace:org/apache/axis/client/Service
	at org.apache.axis.message.SOAPFaultBuilder.createFault(SOAPFaultBuilder.java:222)
	at org.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder.java:129)
	at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087)
	at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(Unknown Source)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
	at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
	at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
	at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
	at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
	at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(Unknown Source)
	at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)
	at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)
	at org.apache.axis.Message.getSOAPEnvelope(Message.java:435)
	at org.apache.axis.handlers.soap.MustUnderstandChecker.invoke(MustUnderstandChecker.java:62)
	at org.apache.axis.client.AxisClient.invoke(AxisClient.java:206)
	at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
	at org.apache.axis.client.Call.invoke(Call.java:2767)
	at org.apache.axis.client.Call.invoke(Call.java:2443)
	at org.apache.axis.client.Call.invoke(Call.java:2366)
	at org.apache.axis.client.Call.invoke(Call.java:1812)
	at com.pcitc.lis.ws.lisforcrm.LisForCrmWebServicePortBindingStub.createPickupOrder(LisForCrmWebServicePortBindingStub.java:345)
	at com.pcitc.lis.ws.lisforcrm.LisForCrmWebServiceDelegateProxy.createPickupOrder(LisForCrmWebServiceDelegateProxy.java:74)
	at com.pcitc.lis.ws.lisforcrm.test.Test.main(Test.java:24)

	{http://xml.apache.org/axis/}hostname:USER-20150617TZ

org/apache/axis/client/Service
	at org.apache.axis.message.SOAPFaultBuilder.createFault(SOAPFaultBuilder.java:222)
	at org.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder.java:129)
	at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087)
	at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(Unknown Source)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
	at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
	at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
	at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
	at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
	at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(Unknown Source)
	at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)
	at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)
	at org.apache.axis.Message.getSOAPEnvelope(Message.java:435)
	at org.apache.axis.handlers.soap.MustUnderstandChecker.invoke(MustUnderstandChecker.java:62)
	at org.apache.axis.client.AxisClient.invoke(AxisClient.java:206)
	at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
	at org.apache.axis.client.Call.invoke(Call.java:2767)
	at org.apache.axis.client.Call.invoke(Call.java:2443)
	at org.apache.axis.client.Call.invoke(Call.java:2366)
	at org.apache.axis.client.Call.invoke(Call.java:1812)
	at com.pcitc.lis.ws.lisforcrm.LisForCrmWebServicePortBindingStub.createPickupOrder(LisForCrmWebServicePortBindingStub.java:345)
	at com.pcitc.lis.ws.lisforcrm.LisForCrmWebServiceDelegateProxy.createPickupOrder(LisForCrmWebServiceDelegateProxy.java:74)
	at com.pcitc.lis.ws.lisforcrm.test.Test.main(Test.java:24)

这种异常不是程序抛出来的,是由服务器抛出的,所以在程序里抓不到异常。

出现此种问题要注意一下是否已经把接口中用到的jar包加进了服务器中或者项目中。

我用的webservice接口的服务器为TOMCAT,此时需要将webservice接口中用到的jar到全部放到tomcat的lib下,这样做后,异常解决


最后

以上就是无限汽车为你收集整理的java调用webservice WSDL形式接口之apache axis方式调用(一)的全部内容,希望文章能够帮你解决java调用webservice WSDL形式接口之apache axis方式调用(一)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(43)

评论列表共有 0 条评论

立即
投稿
返回
顶部