简体   繁体   中英

How to generate request and response classes from wsdl?

I have a Spring Boot app built with Maven. Now, I plan to make requests to the SOAP service from this app. SOAP service is defined by WSDL. I am able to make requests and get responses with this method:

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();
}

However, my request and response here are XML strings. How can I generate my request and response classes from WSDL, so I can use them to make requests with HttpURLConnection ?

Using Apache CXF did not help, as I should use HttpURLConnection . It is one of the requirements of the consumed service.

I can generate classes with JAXB or JAX-WS, but sending generated appropriate classes as request body gave me 400 Bad Request error.

You should use a code generator. You can find more information on WSDL2Java in the CXF documentation: https://cxf.apache.org/docs/how-do-i-develop-a-client.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM