1

SoapUI でテストした SOAP リクエストがあります。目的の応答を正常に取得します。

私のコードの問題は、以下で応答が成功したかどうかをテストしているときに発生しif (isSuccessResponse(soapResponse)) {ます。

public static void main(String[] args) throws Exception {

 // Establish SOAP connection
 SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
 SOAPConnection soapConnection = soapConnectionFactory.createConnection();

 // Generate SOAP request
 SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), namespaceURI);

 // Test to see if SOAP response is available
 System.out.print("SOAP Response:");
 System.out.println();
 soapResponse.writeTo(System.out);

 // Close connection
 soapConnection.close();

 if (isSuccessResponse(soapResponse)) {
  System.out.println("Success!");
 } else {
  System.out.println("Fault!");
 }

}

成功?

private static boolean isSuccessResponse(SOAPMessage soapResponse) throws Exception {
 NodeList responseNodes = soapResponse.getSOAPBody().getElementsByTagNameNS("*", "HelpDesk_QueryList_Service");

 if (responseNodes.getLength() == 1)
  return true;
 else
  return false;
}

リクエスト: このコードは、SoapUI で動作するリクエストと同じであるため、実際には正しいリクエストを出力します。では、実際の応答から関連する関連情報を取得できないのはなぜですか? 印刷された応答は、すべての必要なヘッダーを含む空白のデフォルト応答ですが、要求からの必要な情報はありません。

private static SOAPMessage createSOAPRequest() throws Exception {
 MessageFactory messageFactory = MessageFactory.newInstance();

 // Create SOAP Message
 SOAPMessage soapMessage = messageFactory.createMessage();

 // Add request envelope, headers, and nodes from SOAP request 
 SOAPPart soapPart = soapMessage.getSOAPPart();

 // I used ? here for privacy reasons
 String xmlns = "?";
 String username = "?";
 String password = "?";
 String qualification = "'?";

 SOAPEnvelope envelope = soapPart.getEnvelope();
 envelope.addNamespaceDeclaration("urn", xmlns);

 SOAPHeader soapHeader = envelope.getHeader();
 SOAPElement authInfoElem = soapHeader.addChildElement("AuthenticationInfo", "urn");
 createElementAndSetText(authInfoElem, "userName", username);
 createElementAndSetText(authInfoElem, "password", password);

 SOAPBody soapBody = envelope.getBody();
 SOAPElement bodyElem = soapBody.addChildElement("HelpDesk_QueryList_Service", "urn");
 createElementAndSetText(bodyElem, "Qualification", qualification);

 MimeHeaders headers = soapMessage.getMimeHeaders();
 headers.addHeader("SOAPAction", xmlns + "/HelpDesk_QueryList_Service");

 // Save request
 soapMessage.saveChanges();

 // Print the request message
 System.out.print("SOAP Request:");
 System.out.println();
 soapMessage.writeTo(System.out);
 System.out.println();
 System.out.println();

 return soapMessage;
}

エラー:次のエラーが表示されますが、理由がわかりません。

Lis 07, 2016 6:10:34 ODP. com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl lookForEnvelope
SEVERE: SAAJ0514: Unable to create envelope from given source because the root element is not named Envelope
Lis 07, 2016 6:10:34 ODP. com.sun.xml.internal.messaging.saaj.soap.EnvelopeFactory createEnvelope
SEVERE: SAAJ0511: Unable to create envelope from given source
Exception in thread "main" com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Unable to create envelope from given source: 
    at com.sun.xml.internal.messaging.saaj.soap.EnvelopeFactory.createEnvelope(EnvelopeFactory.java:117)
    at com.sun.xml.internal.messaging.saaj.soap.ver1_1.SOAPPart1_1Impl.createEnvelopeFromSource(SOAPPart1_1Impl.java:69)
    at com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl.getEnvelope(SOAPPartImpl.java:128)
    at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.getSOAPBody(MessageImpl.java:1351)
    at com.xxxx.automation.remedy.RemedySOAPService.isSuccessResponse(RemedySOAPService.java:135)
    at com.xxxx.automation.remedy.RemedySOAPService.main(RemedySOAPService.java:52)
Caused by: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Unable to create envelope from given source because the root element is not named "Envelope"
    at com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl.lookForEnvelope(SOAPPartImpl.java:154)
    at com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl.getEnvelope(SOAPPartImpl.java:121)
    at com.sun.xml.internal.messaging.saaj.soap.EnvelopeFactory.createEnvelope(EnvelopeFactory.java:110)
    ... 5 more

CAUSE:

com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Unable to create envelope from given source because the root element is not named "Envelope"
    at com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl.lookForEnvelope(SOAPPartImpl.java:154)
    at com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl.getEnvelope(SOAPPartImpl.java:121)
    at com.sun.xml.internal.messaging.saaj.soap.EnvelopeFactory.createEnvelope(EnvelopeFactory.java:110)
    at com.sun.xml.internal.messaging.saaj.soap.ver1_1.SOAPPart1_1Impl.createEnvelopeFromSource(SOAPPart1_1Impl.java:69)
    at com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl.getEnvelope(SOAPPartImpl.java:128)
    at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.getSOAPBody(MessageImpl.java:1351)
    at com.xxxx.automation.remedy.RemedySOAPService.isSuccessResponse(RemedySOAPService.java:135)
    at com.xxxx.automation.remedy.RemedySOAPService.main(RemedySOAPService.java

:52)

SOAP 応答:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Body>
      <ns0:HelpDesk_QueryList_ServiceResponse xmlns:ns0="urn:XX:XXXX:HPD_IncidentInterface_WS">
         <ns0:getListValues>
            <ns0:Assigned_Group>XXXX</ns0:Assigned_Group>
            <ns0:Assigned_Group_Shift_Name/>
            <ns0:Assigned_Support_Company>XXXX</ns0:Assigned_Support_Company>
            <ns0:Assigned_Support_Organization>XXXX</ns0:Assigned_Support_Organization>
            <ns0:Assignee/>
            <ns0:Categorization_Tier_1/>
            <ns0:Categorization_Tier_2/>
            <ns0:Categorization_Tier_3/>
            <ns0:City/>
            <ns0:Closure_Manufacturer/>
            <ns0:Closure_Product_Category_Tier1>XXXX</ns0:Closure_Product_Category_Tier1>
            <ns0:Closure_Product_Category_Tier2>XXXX</ns0:Closure_Product_Category_Tier2>
            <ns0:Closure_Product_Category_Tier3/>
            <ns0:Closure_Product_Model_Version/>
            <ns0:Closure_Product_Name/>
            <ns0:Company>XXXX</ns0:Company>
            <ns0:Contact_Company>XXXX</ns0:Contact_Company>
            <ns0:Contact_Sensitivity>Standard</ns0:Contact_Sensitivity>
            <ns0:Country/>
            <ns0:Department>XXXX</ns0:Department>
            <ns0:Summary>XXXX</ns0:Summary>
            <ns0:Notes/>
            <ns0:First_Name>XXXX</ns0:First_Name>
            <ns0:Impact>4-Minor/Localized</ns0:Impact>
            <ns0:Incident_Number>XXXX</ns0:Incident_Number>
            <ns0:Internet_E-mail>XXXX</ns0:Internet_E-mail>
            <ns0:Last_Name>XXXX</ns0:Last_Name>
            <ns0:Manufacturer/>
            <ns0:Organization>XXXX</ns0:Organization>
            <ns0:Phone_Number>XXXX</ns0:Phone_Number>
            <ns0:Priority>Low</ns0:Priority>
            <ns0:Priority_Weight>0</ns0:Priority_Weight>
            <ns0:Product_Categorization_Tier_1>XXXX</ns0:Product_Categorization_Tier_1>
            <ns0:Product_Categorization_Tier_2>XXXX</ns0:Product_Categorization_Tier_2>
            <ns0:Product_Categorization_Tier_3/>
            <ns0:Product_Model_Version/>
            <ns0:Product_Name/>
            <ns0:Region/>
            <ns0:Reported_Source xsi:nil="true"/>
            <ns0:Resolution/>
            <ns0:Resolution_Category/>
            <ns0:Resolution_Category_Tier_2/>
            <ns0:Resolution_Category_Tier_3/>
            <ns0:Service_Type>User Service Restoration</ns0:Service_Type>
            <ns0:Site>XXXX</ns0:Site>
            <ns0:Site_Group>XXXX</ns0:Site_Group>
            <ns0:Status>Cancelled</ns0:Status>
            <ns0:Status_Reason xsi:nil="true"/>
            <ns0:Urgency>4-Low</ns0:Urgency>
            <ns0:VIP>No</ns0:VIP>
            <ns0:ServiceCI/>
            <ns0:ServiceCI_ReconID/>
            <ns0:HPD_CI/>
            <ns0:HPD_CI_ReconID/>
            <ns0:HPD_CI_FormName/>
            <ns0:z1D_CI_FormName/>
            <ns0:Vendor_Ticket_Number xsi:nil="true"/>
            <ns0:Corporate_ID>XXXX</ns0:Corporate_ID>
            <ns0:Submitter>XXXX</ns0:Submitter>
            <ns0:Pending_Date xsi:nil="true"/>
         </ns0:getListValues>
      </ns0:HelpDesk_QueryList_ServiceResponse>
   </soapenv:Body>
</soapenv:Envelope>
4

1 に答える 1

1

まず、SAAJ フレームワークが応答を処理しているときにエラーが発生したようです。したがって、リクエストの作成については掘り下げません。特に、SoapUI が作成したものと同じであると主張しているので、問題ないと思います。HTTPヘッダーをチェックして、それをさらに確認することもできます.

スタック トレースの重要な部分は次のとおりです。

at com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl.getEnvelope(SOAPPartImpl.java:128)
at com.sun.xml.internal.messaging.saaj.soap.MessageImpl.getSOAPBody(MessageImpl.java:1351)

これは、SOAP フレームワークが応答エンベロープを処理できなかったことを示しています。応答が実際に有効であると仮定すると、これは疑わしく見えます。

あなたのコードを見ると、これは SAAJ API の誤用であると思われます:

// Close connection
soapConnection.close();

if (isSuccessResponse(soapResponse)) {
  System.out.println("Success!");
} else {
  System.out.println("Fault!");
}

メッセージを実際に使用する前に、接続を閉じます。SAAJ の実装が遅延している場合 (これは当然だと思います)、メッセージはネットワーク Socket からすぐに解析されません。応答メッセージを使用する前に接続を閉じると、ソケットが読み取られる前に実際に閉じられ、その結果、実装はメッセージを読み取ることができなくなります。

応答オブジェクトを使用したまで、接続の終了を遅らせることをお勧めします。

コードのこの部分についても、同じ議論を行うことができます。

 System.out.print("SOAP Response:");
 System.out.println();
 soapResponse.writeTo(System.out);

 // Close connection
 soapConnection.close();

応答をコンソールにダンプすることで、実際にフレームワークにソケットを消費させます (HTTP 応答を読み取ります)。後で、フレームワークに応答ストリームをエンベロープとして解析するように要求すると、ストリームのコンテンツがキャッシュされていない場合、この操作は失敗します。解析するバイトがありません。

一言で言えば、フレームワークが実際に結果を解析する機会を得る前に、接続または応答オブジェクトに「触れない」でください。これは API 設計の観点からは明らかではありませんが、理解できる動作です。

他のいくつかのリード: スタックオーバーフローの他の人は、古いバージョンの Xerces/Xalan が原因であると報告しています (これらは XML パーサーであり、JDK には独自のバージョンの Xerces が組み込まれています)。これも確認してください: Java Spring WS org.springframework.ws.soap.saaj.SaajSoapEnvelopeException: エンベロープにアクセスできませんでした http://mmmsoftware.blogspot.fr/2009/06/xml-namespace-error-with-spring-ws.html

于 2016-11-07T17:34:39.490 に答える