【问题标题】:How to generate request and response classes from wsdl?如何从 wsdl 生成请求和响应类?
【发布时间】:2020-04-11 08:36:27
【问题描述】:

我有一个使用 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 错误。

【问题讨论】:

    标签: java spring-boot soap jaxb cxf


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2016-06-24
      • 1970-01-01
      • 2023-03-15
      • 2023-02-06
      • 2014-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多