当我在createSOAPResponse
中打印响应时,我确实得到了正确的响应体,但我没有得到实际值。我将获得一个像<xsd:element name="Incident_Number" type="xsd:string"/>
这样的节点元素,但没有相关的值。我是新来的。我错过了任何步骤吗?
为了清晰起见,我已经包含了我班级的完整源代码:
public class RemedySOAPService {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// Establish SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Create SOAP Request
String namespaceURI = "XXX";
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), namespaceURI);
// Create SOAP Response
createSOAPResponse(soapResponse);
// Close Connection
soapConnection.close();
}
/**
* @param soapResponse
* @throws Exception
*/
private static void createSOAPResponse(SOAPMessage soapResponse) throws Exception {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
Source sourceContent = soapResponse.getSOAPPart().getContent();
System.out.println("SOAP Response:");
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
}
/**
* @return soapMessage
* @throws Exception
*/
private static SOAPMessage createSOAPRequest() throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
// Create SOAP Message
SOAPMessage soapMessage = messageFactory.createMessage();
/*<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="XXX">
<soapenv:Header>
<urn:AuthenticationInfo>
<urn:userName>XXX</urn:userName>
<urn:password>XXX</urn:password>
</urn:AuthenticationInfo>
</soapenv:Header>
<soapenv:Body>
<urn:HelpDesk_QueryList_Service>
<urn:Qualification>XXX</urn:Qualification>
</urn:HelpDesk_QueryList_Service>
</soapenv:Body>
</soapenv:Envelope>*/
// Create SOAP Part
SOAPPart soapPart = soapMessage.getSOAPPart();
// Set Constants
String xmlns = "XXX";
String username = "XXX";
String password = "XXX";
String qualification = "XXX";
// Create SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("urn", xmlns);
// Create SOAP Header
SOAPHeader soapHeader = envelope.getHeader();
// Create SOAP Element for AuthenticationInfo
SOAPElement authInfoElem = soapHeader.addChildElement("AuthenticationInfo", "urn");
// Create SOAP ChildElements for userName and password
SOAPElement usernameChild = authInfoElem.addChildElement("userName", "urn");
SOAPElement passwordChild = authInfoElem.addChildElement("password", "urn");
// Assign values to SOAP ChildElements
usernameChild.addTextNode(username);
passwordChild.addTextNode(password);
// Create SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement bodyElem = soapBody.addChildElement("HelpDesk_QueryList_Service", "urn");
createElementAndSetText(bodyElem, "Qualification", qualification);
// Create MimeHeaders
String namespaceURI = "XXX";
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", namespaceURI + "/XXX");
// Save Request
soapMessage.saveChanges();
// Print the Request Message
printSOAPRequest(soapMessage);
return soapMessage;
}
/**
* @param parent
* @param element
* @param text
* @return
* @throws SOAPException
*/
private static SOAPElement createElementAndSetText(SOAPElement parent, String element, String text)
throws SOAPException {
SOAPElement child = parent.addChildElement(element, "urn");
child.addTextNode(text);
return child;
}
/**
* @param soapMessage2
* @throws SOAPException
* @throws IOException
*/
private static void printSOAPRequest(SOAPMessage soapMessage) throws SOAPException, IOException {
System.out.print("SOAP Request:");
System.out.println();
soapMessage.writeTo(System.out);
System.out.println();
System.out.println();
}
}
我的目标是收集从SOAP响应返回的列表,并在第二个操作中使用它们。但是,如果它们没有出现,我如何从该元素中提取值?
我已在SoapUI中测试过,所以我确信请求和响应是正确的。我在构建客户端时遇到了麻烦。