访问Java Web Service并获得响应

时间:2014-02-20 03:22:15

标签: java xml web-services wsdl

我正在做一个项目,它必须访问给定的Web服务并将数据发送到请求的参数并从服务获得响应。目前我做了一些我在论坛上找到的东西,它将数据解析为服务,但由于它给出错误而无法得到响应。 我的示例代码如下。

package webserviceexample;
import javax.xml.soap.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
public class aaaa {
public static void main(String args[]) {
    try {           
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection soapConnection = soapConnectionFactory.createConnection();
         String url = "https://vlmt.XXX.lk:0111/VendorRecharge/services/RechargeVendorService/wsdl/";
        SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
        printSOAPResponse(soapResponse);
        //soapConnection.close();
    } catch (Exception e) {
        System.err.println("Error occurred while sending SOAP Request to Server");
        e.printStackTrace();
    }
}
private static SOAPMessage createSOAPRequest() throws Exception {
    MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage soapMessage = messageFactory.createMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();
    String serverURI = "http://vendor.XXX.ZZZ.com/";

    SOAPEnvelope envelope = soapPart.getEnvelope();
    envelope.addNamespaceDeclaration("wsdl", serverURI);

    SOAPBody soapBody1 = envelope.getBody();
    SOAPElement soapBodyElemX = soapBody1.addChildElement("performRecharge", "wsdl");

    SOAPElement soapBodyElem1 = soapBodyElemX.addChildElement("storeId", "wsdl");
    soapBodyElem1.addTextNode("LA19BNPX");
    SOAPElement soapBodyElem2 = soapBodyElemX.addChildElement("password", "wsdl");
    soapBodyElem2.addTextNode("thilanka@456A");
    SOAPElement soapBodyElem3 = soapBodyElemX.addChildElement("mobileNo", "wsdl");
    soapBodyElem3.addTextNode("751238456");
    SOAPElement soapBodyElem4 = soapBodyElemX.addChildElement("Amount", "wsdl");
    soapBodyElem4.addTextNode("12");
    SOAPElement soapBodyElem5 = soapBodyElemX.addChildElement("vendorTransactionId", "wsdl");
    soapBodyElem5.addTextNode("15");

    MimeHeaders headers = soapMessage.getMimeHeaders();
    headers.addHeader("performRecharge", serverURI + "performRecharge");
    System.out.println("");
    soapMessage.saveChanges();

    System.out.print("Request SOAP Message = ");
    soapMessage.writeTo(System.out);
    System.out.println();
    return soapMessage;
}

private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    Source sourceContent = soapResponse.getSOAPPart().getContent();
    System.out.print("\nResponse SOAP Message = ");
    StreamResult result = new StreamResult(System.out);
    transformer.transform(sourceContent, result);
}

}

我得到的错误是:

请求SOAP消息

<SOAP-ENV:Envelope xmlns:SOAP-    ENV="http://schemas.xmlsoap.org/soap/envelope/"   
  xmlns:wsdl="http://vendor.virtualization.ibm.com/">
  <SOAP-ENV:Header/>
  <SOAP-ENV:Body>
    <wsdl:performRecharge>
      <wsdl:storeId>LA19BNPX</wsdl:storeId> 
      <wsdl:password>thilanka@456A</wsdl:password>
      <wsdl:mobileNo>751238456</wsdl:mobileNo>
      <wsdl:Amount>12</wsdl:Amount> 
    <wsdl:vendorTransactionId>15</wsdl:vendorTransactionId>
   </wsdl:performRecharge>
  </SOAP-ENV:Body>
 </SOAP-ENV:Envelope>

回复(错误)

<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope   
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"    
  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Header/>
  <soapenv:Body>
    <soapenv:Fault xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
      <faultcode>soapenv:Server.generalException</faultcode>   
      <faultstring>javax.xml.rpc.JAXRPCException: WSWS3122E: Error: Could not find service services/RechargeVendorService/wsdl/ referenced in URI /VendorRecharge/services/RechargeVendorService/wsdl/</faultstring>
   </soapenv:Fault>
  </soapenv:Body>
</soapenv:Envelope>

我必须在Web服务中调用“performRecharge”。请帮我。 Web服务摘要如下。

请帮我克服这一点。这很重要。 谢谢。

WSDL文件

1 个答案:

答案 0 :(得分:1)

确保这是正确的服务网址。

好像你指定了wsdl位置而没有注册服务网址。设置wsdl文件中为服务指定的请求URL。

String url = "https://vlmt.XXX.lk:0111/VendorRecharge/services/RechargeVendorService/wsdl/";

查看wsdl中的位置标记

<service name="Hello_Service">
      <documentation>WSDL File for HelloService</documentation>
      <port binding="tns:Hello_Binding" name="Hello_Port">
         <soap:address
            location="**http://www.examples.com/SayHello/**">
      </port>
   </service>