Java 如何从wsdl生成请求和响应类?
我有一个用Maven构建的Spring Boot应用程序。现在,我计划从这个应用程序向SOAP服务发出请求。SOAP服务由WSDL定义。我可以使用此方法发出请求并获得响应:Java 如何从wsdl生成请求和响应类?,java,spring-boot,soap,jaxb,cxf,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 = ur
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
的请求
使用apachecxf没有帮助,因为我应该使用HttpURLConnection
。这是消费服务的需求之一
我可以使用JAXB或JAX-WS生成类,但将生成的适当类作为请求体发送给我会导致400个错误请求。您应该使用代码生成器。您可以在CXF文档中找到有关WSDL2Java的更多信息: