繁体   English   中英

如何从 wsdl 生成请求和响应类?

[英]How to generate request and response classes from wsdl?

我有一个用 Maven 构建的 Spring Boot 应用程序。 现在,我计划从这个应用程序向 SOAP 服务发出请求。 SOAP 服务由 WSDL 定义。 我可以使用此方法发出请求并获得响应:

private String makeSoapRequest(String request, String action) throws IOException {
    URL url = new URL("http://localhost/");
    URLConnection urlConnection = url.openConnection();
    HttpURLConnection httpURLConnection = (HttpURLConnection)urlConnection;


    InputStream is = new ByteArrayInputStream(request.getBytes());
    baos = new ByteArrayOutputStream();

    copy(is, baos);
    is.close();

    byte[] bytes = baos.toByteArray();

    httpURLConnection.setRequestProperty("Content-Length", String.valueOf( bytes.length ) );
    httpURLConnection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
    httpURLConnection.setRequestProperty("SOAPAction", action);
    httpURLConnection.setRequestMethod("POST");
    httpURLConnection.setDoOutput(true);
    httpURLConnection.setDoInput(true);

    OutputStream out = httpURLConnection.getOutputStream();
    out.write(bytes);
    out.close();

    InputStreamReader isr = new InputStreamReader(httpURLConnection.getInputStream());
    BufferedReader in = new BufferedReader(isr);

    String inputLine;
    StringBuilder sb = new StringBuilder();
    while ((inputLine = in.readLine()) != null) {
        ab.append(inputLine);
    }

    return sb.ToString();
}

但是,我这里的请求和响应是 XML 字符串。 如何从 WSDL 生成我的请求和响应类,以便我可以使用它们通过HttpURLConnection发出请求?

使用 Apache CXF 没有帮助,因为我应该使用HttpURLConnection 它是被消费服务的要求之一。

我可以使用 JAXB 或 JAX-WS 生成类,但是将生成的适当类作为请求正文发送给我 400 Bad Request 错误。

您应该使用代码生成器。 您可以在 CXF 文档中找到有关 WSDL2Java 的更多信息: https : //cxf.apache.org/docs/how-do-i-develop-a-client.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM