【发布时间】: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