2016-07-25 101 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“,我想知道如何调用这个方法正确地发送参数。

谢谢!

回答