2016-07-25 98 views
0

如何讓一個java客戶端調用帶有參數的soap WebService方法?如何讓一個java客戶端使用參數調用soap WebService方法?

我試過這個類Java客戶端

import javax.xml.soap.*; 

public class SOAPClientSAAJ { 

public static void main(String args[]) throws Exception { 
    // Create SOAP Connection 
    SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); 
    SOAPConnection soapConnection = soapConnectionFactory.createConnection(); 

    // Send SOAP Message to SOAP Server 
    String url = "http:localhost:8080/myproject/mywebservice?wsdl"; 
    SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url); 

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

    soapConnection.close(); 
} 

private static SOAPMessage createSOAPRequest() throws Exception { 
    MessageFactory messageFactory = MessageFactory.newInstance(); 
    SOAPMessage soapMessage = messageFactory.createMessage(); 
    SOAPPart soapPart = soapMessage.getSOAPPart(); 

    String namespace= "http://wsnamespace/"; 

    // SOAP Envelope 
    SOAPEnvelope envelope = soapPart.getEnvelope(); 
    envelope.addNamespaceDeclaration("example", namespace); 

    // SOAP Body 
    SOAPBody soapBody = envelope.getBody(); 
    SOAPElement soapBodyElem = soapBody.addChildElement("Login", "example"); 
    SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("username", "example"); 
    SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("password", "example"); 
    soapBodyElem1.addTextNode("[email protected]"); 
    soapBodyElem2.addTextNode("1234"); 

    MimeHeaders headers = soapMessage.getMimeHeaders(); 
    headers.addHeader("SOAPAction", namespace + "Login"); 

    soapMessage.saveChanges(); 

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

    return soapMessage; 
} 

} 

與該Java Web服務方法

@WebMethod(operationName="Login") 
public boolean Login(@WebParam(name = "username") String username, 
@WebParam(name = "password") String password) { 
    System.out.print(username + "-" + password); 
} 

,但用戶名和密碼總是空,因此當該方法被稱爲輸出爲「 null-null「,我想知道如何調用這個方法正確地發送參數。

謝謝!

回答