1

There is a service endpoint and an xsd file for SOAP request. But, there is no wsdl file. How can I generate soap request (xml request as a string ) manually from this and send it to the service endpoint?

I found similar answer on SO. But it's for C# and .NET. Any idea for Java will be highly appreciated.

Community
  • 1
  • 1
WEB
  • 11
  • 3
  • possible duplicate of [SOAP request to WebService with java](http://stackoverflow.com/questions/19291283/soap-request-to-webservice-with-java) – Dávid Szabó Jul 30 '14 at 19:24
  • 1
    It's not duplicate as I wanted to create the request manually without using SAAJ. – WEB Jul 30 '14 at 19:35

2 Answers2

2

Take a look at [JAXB]: https://jaxb.java.net/ It does stuff like you are asking. It generates java classes if needed:

the command xjc is the key for generation

cilap
  • 2,215
  • 1
  • 25
  • 51
  • I want to create the xml request manually. I don't want to add class files to access the remote methods. – WEB Jul 30 '14 at 20:01
  • I do not understand why someone is not willing to use the features your Environment provides you. Which jdk version are you using? – cilap Jul 30 '14 at 20:05
  • It's given from a third party. Only the xsd file and service endpoint have been given. – WEB Jul 30 '14 at 20:21
  • what version of JDK are you using? – cilap Jul 30 '14 at 20:27
  • JAXB is part of JDK since 1.6 so please try: xjc -p yourJavaPackage http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd It will generate you a bunch of classes you can directly use. All part of java standard none additional jar – cilap Jul 30 '14 at 20:46
1

Here is an example from an old project of mine connecting to a SharePoint web service. It should show you all the basics that you need.

try {
    URL sharepoint = new URL("http://server.com/_vti_bin/Lists.asmx");
    URLConnection sharepoint_connection = sharepoint.openConnection();

    String body = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
        "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
        "  <soap:Body>" +
        "  </soap:Body>" +
        "</soap:Envelope>";
    System.out.println("~~~~~ Roadmap: " + body);

    sharepoint_connection.setRequestProperty("Man", "POST /_vti_bin/Lists.asmx HTTP/1.1");
    sharepoint_connection.setRequestProperty("Host", "server.com");
    sharepoint_connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
    sharepoint_connection.setRequestProperty("Content-Length", Integer.toString(body.length()));
    sharepoint_connection.setRequestProperty("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/GetListItems");

    // Write request body to SharePoint
    sharepoint_connection.setDoOutput(true);
    OutputStreamWriter writer = new OutputStreamWriter(sharepoint_connection.getOutputStream());
    writer.write(body);
    writer.close();

    //sharepoint_connection.connect();

    // Read result from SharePoint
    BufferedReader reader = new BufferedReader(new InputStreamReader(sharepoint_connection.getInputStream()));
    String inputLine;
    while ((inputLine = reader.readLine()) != null)
        xmltext += inputLine;
    reader.close();
} catch (MalformedURLException e) {     // new URL() failed
} catch (IOException e) {               // openConnection() failed
}
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52