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

标签 java spring-boot soap jaxb cxf

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

关于java - 如何从 wsdl 生成请求和响应类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59397996/

相关文章:

php - SoapFault 异常 : [HTTP] Error Fetching http headers

php - 如何在 PHP 7.0 Ubuntu 18.10 中启用 SoapClient

java - 动态多维 Java 结构替换静态多维数组

java - 如何分享listiview的内容(图片+文字)?

ssl - Spring-Boot 客户端认证配置。

java - Bean创建异常: Error creating bean with name 'userRepository' : Post-processing of merged bean definition failed

node.js - Nodejs Soap api 调用返回 "Unexpected root element of WSDL or include"

Java:合并 2 个图像不起作用

java - Android Studio 无法解析 android.gms 包中缺少的常见符号

spring - 如何绕过 RestController 上的 Spring @PreAuthorize 注解进行测试?