java 调用 wsdl形式的webservice

                项目中有这样一个需求,调用运营商接口,给用户充值流量。但是接口地址是wsdl形式的。之前只是在.net开发的时候用WCF调用过接口。那么java如何实现调用wsdl形式的webservice呢?

         网上有许多类似的问题,也有一些解决方法,有的说用了XFire解决了,有的说用了AXIS。于是我采用了第二种。


 一、问题解决过程


1、下载AXIS2类库,AXIS2是目前java调用webservice的一个主要方法

          首先说一下使用AXIS调用WSDL文件时需要的JAR包:
        (1)axis    其中包括axis.jar、axis-ant.jar、commons-discovery-0.2.jar、commons-logging-1.0.4.jar、jaxrpc.jar、log4j-1.2.8.jar、log4j.properties、saaj.jar、wsdl4j-1.5.1.jar。

        (2)mail.jar。
        (3)activation.jar。


2、由于是第三方webservice,直接引入AXIS2的包就可以了。 在调用和调错期间,我主要引了这么多的jar包:

    
                  java 调用 wsdl形式的webservice_第1张图片

3、代码:

    /**
     * description:调用运营商充值接口方法
     * author:xuxiaoguai
     * 2015-12-1 10:32:04
     * @return 返回值参数代码
     */
    public String invokeRemoteFuc() {
        String endpoint="http://^^^^";
        String result="no result!";
        System.out.println(result);

        Service service =new Service();
        Call call;

        Object[] object=new Object[4];  //Object用来存储方法的参数
        object[0]="XXX";  //相应的参数,由于保密
        object[1]="XXX";
        object[2]="XXX";
        object[3]="10M";

        try {
            call=(Call)service.createCall();
            call.setTargetEndpointAddress(endpoint); //远程调用路径
            call.setOperationName("methodname");  //调用的方法名

            //设置参数名:
            call.addParameter("XX", //参数名
                    XMLType.XSD_STRING,//参数类型:String
                    ParameterMode.IN); //参数模式:'IN' or 'OUT'
            call.addParameter("XX", XMLType.XSD_STRING,ParameterMode.INOUT);
            call.addParameter("XX", XMLType.XSD_STRING,ParameterMode.INOUT);
            call.addParameter("XX", XMLType.XSD_STRING,ParameterMode.INOUT);

            //设置返回值类型:
            call.setReturnType(XMLType.XSD_STRING);   //返回值类型:String
            result=(String) call.invoke(object);  //远程调用
        }catch (ServiceException e) {
            e.printStackTrace();
        }catch (RemoteException e){
            e.printStackTrace();
        }
        return result;
    }


    //测试充值接口方法-xuxiaoguai-2015年11月30日17:13:04
    public static void main(String[] args){
        BusinessController t=new BusinessController();
        String result =t.invokeRemoteFuc();
        System.out.println(result);
    }
            代码说明:由于接口和参数很重要,不能外漏,所以小编用了XX代替,在object数组中是对应参数的值,在
                   call.addParameter("XX", XMLType.XSD_STRING,ParameterMode.INOUT);
中对应的XX是指接口参数名称,我们还是分清一些的好。


二、一些错误


        当然,小编也遇到了一些错误,都是一些缺少包的情况,我们去下载导入相应的jar包就好,但是还有一个问题作为附件吧,贴上来:

附:
 AxisFault
 faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.userException
 faultSubcode: 
 faultString: org.xml.sax.SAXException: Bad envelope tag:  definitions
 faultActor: 
 faultNode: 
 faultDetail: 
	{http://xml.apache.org/axis/}stackTrace:org.xml.sax.SAXException: Bad envelope tag:  definitions
	at org.apache.axis.message.EnvelopeBuilder.startElement(EnvelopeBuilder.java:71)
	at org.apache.axis.encoding.DeserializationContext.startElement(DeserializationContext.java:1048)
	at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
	at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:379)
	at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:605)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3138)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:880)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
	at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
	at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
	at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
	at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:649)
	at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:333)
	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.demo.controller.business.BusinessController.invokeRemoteFuc(BusinessController.java:107)
	at com.demo.controller.business.BusinessController.main(BusinessController.java:120)

	{http://xml.apache.org/axis/}hostname:aa-PC

org.xml.sax.SAXException: Bad envelope tag:  definitions
	at org.apache.axis.AxisFault.makeFault(AxisFault.java:101)
	at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:701)
	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.demo.controller.business.BusinessController.invokeRemoteFuc(BusinessController.java:107)
	at com.demo.controller.business.BusinessController.main(BusinessController.java:120)
Caused by: org.xml.sax.SAXException: Bad envelope tag:  definitions
	at org.apache.axis.message.EnvelopeBuilder.startElement(EnvelopeBuilder.java:71)
	at org.apache.axis.encoding.DeserializationContext.startElement(DeserializationContext.java:1048)
	at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
	at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:379)
	at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:605)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3138)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:880)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
	at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
	at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
	at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
	at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
	at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
	at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:649)
	at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:333)
	at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)
	at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)
	... 10 more

           解决的方法是:去掉接口地址的 ?wsdl 

          中间还会报一些其他的错误,也就是参数不对应等参数错误了……



          这种方式是直接直接AXIS调用远程的web service ,还有直接SOAP调用远程的webservice 的方法,还有使用wsdl2java把WSDL文件转成本地类,然后像本地类一样使用,但是后两种方法小编还没有尝试。

你可能感兴趣的:(●项目实战,-------【java】)