如何在java中生成和发送soap请求

时间:2014-07-30 19:19:05

标签: java soap

SOAP请求有一个服务端点和一个xsd文件。但是,没有wsdl文件。如何从此手动生成soap请求(xml请求作为字符串)并将其发送到服务端点?

我在SO找到了类似的答案。但它适用于C#和.NET。任何关于Java的想法都将受到高度赞赏。

2 个答案:

答案 0 :(得分:2)

看看[JAXB]:https://jaxb.java.net/ 它确实像你要问的那样。如果需要,它会生成java类:

命令xjc是生成的关键

答案 1 :(得分:1)

以下是我的旧项目连接到SharePoint Web服务的示例。它应该向您展示您需要的所有基础知识。

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
}