使用Eclipse调用Java Call soap标头

时间:2017-05-23 13:05:42

标签: java soap webservice-client

我有以下soap web服务,我试图在java中编写代码。我已添加Web服务客户端,但很难在Web服务中包含标头。这是soap UI的代码。当我通过soap UI传递参数时,我得到了所需的结果。

  <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
  <soapenv:Header>
  <tem:UserCredentials>
     <tem:userName>?</tem:userName>
     <tem:password>?</tem:password>
  </tem:UserCredentials>
 </soapenv:Header>
 <soapenv:Body>
  <tem:VerifyAmount>
     <tem:ID>?</tem:ID>
     <tem:AMOUNT>?</tem:AMOUNT>
  </tem:VerifyAmount>

   

但是当我从eclipse打电话时,我收到错误消息。这是我的主要课程。

import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import org.apache.axis.message.SOAPHeaderElement;
import org.tempuri.TestService;
import org.tempuri.TestServiceStub;
import org.tempuri.VerifyServiceLocator;
public class Program {
public static void main(String[] args) throws SOAPException
    {
    SOAPHeaderElement UserCredentials;
    SOAPElement oElement;
    UserCredentials = new SOAPHeaderElement("urn:thisNamespace","UserCredentials");
    UserCredentials.setPrefix("tem");
    ((SOAPHeaderElement) UserCredentials).setMustUnderstand(false);
    oElement = UserCredentials.addChildElement("userName");
    oElement.addTextNode("system");
    oElement = UserCredentials.addChildElement("password");
    oElement.addTextNode("abc123");

    VerifyServiceLocator verifyService = new VerifyServiceLocator();

    try {

        TestServiceStub stub = new TestServiceStub();
        stub.setHeader(UserCredentials);
        TestService testService = verifyService .getTestService();
            String verifyAmount = testService.verifyAmount("123", "100");



    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
        }
   }

我在跑步后得到的错误看起来像这样

   AxisFault
   faultCode: {http://schemas.xmlsoap.org/soap/envelope/}MustUnderstand
   faultSubcode: 
   faultString: Missing required header 'UserCredentials'.
   faultActor: 
   faultNode: 
   faultDetail: 
   {http://xml.apache.org/axis/}stackTrace:Missing required header 'UserCredentials'.
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(AbstractSAXParser.java:609)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1782)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2967)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:112)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:841)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:770)
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:643)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:327)
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 org.tempuri.TestServiceStub.verifyTxn(TestServiceStub.java:118)
at iBankingWebService.Program.main(Program.java:38)

0 个答案:

没有答案