gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:18:02 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com