将SOAP字符串消息转换为SoapMessage的NullPointer异常

时间:2016-06-15 14:31:00

标签: java xml soap jaxp saaj

我将XML字符串转换为SOAPMessage。但是,在尝试调用服务时,我最终得到NullPointerException。当单步执行代码时,我可以告诉它正在尝试为这个uri名称空间设置一个空前缀:" urn:oasis:names:tc:ebxml-regrep:xsd:query:3.0"。

代码:

    //PORT_NAME={urn:ihe:iti:xds-b:2007}RespondingGateway_Port_Soap12
    //SERVICE_NAME={urn:ihe:iti:xds-b:2007}RespondingGateway_Service
        Service service = Service.create(SERVICE_NAME);
        service.addPort(PORT_NAME, SOAPBinding.SOAP12HTTP_BINDING, endpointUrl);
        Dispatch<SOAPMessage> dispatch = service.createDispatch(PORT_NAME,
                SOAPMessage.class, Service.Mode.MESSAGE);
        squashWSA(dispatch);
        addHttpHeaders(dispatch, messageId);
        //set the request timeout value
        dispatch.getRequestContext().put(JAXWSProperties.CONNECT_TIMEOUT, haimsConnectTimeout);
        dispatch.getRequestContext().put(JAXWSProperties.REQUEST_TIMEOUT, haimsReceiveTimeout);

            byte[] msgBytes = domToBytes(rawSoapRqst);
    MessageFactory mf = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
            SOAPMessage request = mf.createMessage(new MimeHeaders(), new ByteArrayInputStream(msgBytes));
            SOAPMessage response = dispatch.invoke(request);

堆栈跟踪:

 java.lang.NullPointerException
        at com.sun.xml.messaging.saaj.soap.impl.ElementImpl.addNamespaceDeclaration(ElementImpl.java:528)
        at com.sun.xml.ws.api.message.saaj.SaajStaxWriter.writeNamespace(SaajStaxWriter.java:193)
        at com.sun.xml.ws.util.xml.XMLStreamReaderToXMLStreamWriter.handleStartElement(XMLStreamReaderToXMLStreamWriter.java:215)
        at com.sun.xml.ws.util.xml.XMLStreamReaderToXMLStreamWriter.bridge(XMLStreamReaderToXMLStreamWriter.java:124)
        at com.sun.xml.ws.message.stream.StreamMessage.writePayloadTo(StreamMessage.java:381)
        at com.sun.xml.ws.message.stream.StreamMessage.writeEnvelope(StreamMessage.java:417)
        at com.sun.xml.ws.message.stream.StreamMessage.writeTo(StreamMessage.java:395)
        at com.sun.xml.ws.api.message.MessageWrapper.writeTo(MessageWrapper.java:230)
        at com.sun.xml.ws.api.message.saaj.SAAJFactory.readAsSOAPMessage(SAAJFactory.java:279)
        at com.sun.xml.ws.api.message.saaj.SAAJFactory.readAsSAAJ(SAAJFactory.java:206)
        at com.sun.xml.ws.api.message.saaj.SAAJFactory.read(SAAJFactory.java:195)
        at com.sun.xml.ws.message.AbstractMessageImpl.toSAAJ(AbstractMessageImpl.java:200)
        at com.sun.xml.ws.api.message.MessageWrapper.readAsSOAPMessage(MessageWrapper.java:180)
        at com.sun.xml.ws.client.dispatch.SOAPMessageDispatch.toReturnValue(SOAPMessageDispatch.java:104)
        at com.sun.xml.ws.client.dispatch.SOAPMessageDispatch.toReturnValue(SOAPMessageDispatch.java:75)
        at com.sun.xml.ws.client.dispatch.DispatchImpl.doInvoke(DispatchImpl.java:289)
        at com.sun.xml.ws.client.dispatch.DispatchImpl.invoke(DispatchImpl.java:304)
        at com.domain.wrapper.h.Queryh.queryh(Queryh.java:87)
        at com.domain.wrapper.h.QueryhTest.test(QueryhTest.java:60)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

SOAP消息:

<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
       <soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
          <wsa:To>https://service.com:8443/ABC</wsa:To>
          <wsa:Action>urn:ihe:iti:2007:CrossGatewayQuery</wsa:Action>
          <wsa:ReplyTo>
             <wsa:Address>urn:ihe:iti:2007:CrossGatewayQuery</wsa:Address>
          </wsa:ReplyTo>
          <wsa:MessageID>325b321a-5080-11e3-9c3c-6bf842da5cba</wsa:MessageID>
          <!-- These must be namespace-qualified
          <xxx-transaction-id>325d7c0b-5080-11e3-9c3c-6bf842da5cba</xxx-transaction-id>
          <xxx-fullname>Brandon L Cooper</xxx-fullname>
          <xxx-sitename>HAIMS2</xxx-sitename>
          <xxx-sitenumber>0001</xxx-sitenumber>
          <xxx-ssn>999428882</xxx-ssn>
          <xxx-purpose-of-use>Routine Medical Care</xxx-purpose-of-use>
          -->
       </soap:Header>

       <soap:Body>
          <query:AdhocQueryRequest xmlns:query="urn:oasis:names:tc:ebxml-regrep:xsd:query:3.0" xmlns:rim="urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0" xmlns:urn="urn:ihe:iti:xds-b:2007" xmlns:urn2="urn:oasis:names:tc:ebxml-regrep:xsd:query:3.0">
             <rim:AdhocQuery xmlns:urn3="urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0">
                <rim:Slot name="$XDSDocumentEntryPatientId">
                   <rim:ValueList>
                      <rim:Value>'340993^^^&amp;2.16.840.1.113883.3.42.10002.1&amp;ISO'</rim:Value>
                   </rim:ValueList>
                </rim:Slot>
                <rim:Slot name="$XDSDocumentEntryStatus">
                   <rim:ValueList>
                      <rim:Value>'urn:oasis:names:tc:ebxml-regrep:StatusType:Approved'</rim:Value>
                   </rim:ValueList>
                </rim:Slot>
                <rim:Slot name="$XDSDocumentEntryClassCode">
                   <rim:ValueList>
                      <rim:Value>'51851-4'</rim:Value>
                   </rim:ValueList>
                </rim:Slot>
                <rim:Slot name="$XDSDocumentEntryClassCodeScheme">
                   <rim:ValueList>
                      <rim:Value>'LOINC'</rim:Value>
                   </rim:ValueList>
                </rim:Slot>
                <rim:Slot name="$XDSDocumentEntryCreationTimeFrom">
                   <rim:ValueList>
                      <rim:Value/>
                   </rim:ValueList>
                </rim:Slot>
                <rim:Slot name="$XDSDocumentEntryCreationTimeTo">
                   <rim:ValueList>
                      <rim:Value/>
                   </rim:ValueList>
                </rim:Slot>
             </rim:AdhocQuery>
          </query:AdhocQueryRequest>
       </soap:Body>
    </soap:Envelope>

1 个答案:

答案 0 :(得分:0)

这结果是一个Classpath问题。我的应用程序依赖于对Xerces的传递依赖,但WebLogic wlfullclient.jar依赖已经有了JAXP。我使用了依赖&#34;排除&#34;配置删除传递Xerces依赖(groupId of&#34; xerces&#34;和&#34; xml-apis&#34;在我的代码的其他地方),问题就消失了。顺便说一句,在代码更改之前,测试是从命令行开始的,而不是从Eclipse开始。加载课程的差异可能导致不同的结果。