How to send a request to a Webservice from a text file just like SoapUI
This example shows how to create a simple java Soap WS and send a request to it from a text file.
Features:
- It uses the standard JDK 1.8 libraries.
- You can use it for REST webservices but you'll have to change headers accordingly.
- You don't need an application server or web container to run the server side.
Download the source code here
Create the Server Side
package test.sw.server; import javax.jws.WebService; @WebService(endpointInterface = "test.sw.server.TestWSServer") public class TestWSServer { public double cmToIn(double cm) { return cm / 2.54; } public double inToCm(double in) { return in * 2.54; } }
Create the Endpont
package test.sw.server; import javax.xml.ws.Endpoint; public class UCPublisher { public static void main(String[] args) { Endpoint.publish("http://localhost:8080/TestSoap", new TestWSServer()); } }
Run the endpoint
If you load the url http://localhost:8080/TestSoap?wsdl with a web browser or smilar you should see the wsdl soap xml being generated.
Test the WS with SoapUI or similar
File > New Soap Project > Choose a project name and add the http://localhost:8080/TestSoap?wsdl (the endpoint must be executing) Then click ok
Change the arg0 in the request part and click the green triangle to submit request. You should see something like this:
Copy the whole request into a file that will be used in the final example.
Send file to WS just like Soap UI
package test.sw.server; import java.io.DataOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Scanner; public class TestWSHTTPS { private static final int TIME_OUT = 10000; public static void main(String[] args) throws Exception { final String URL = "http://localhost:8080/TestSoap"; final String FILE = "/path/to/your/file.txt"; URL obj = new URL(URL); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setConnectTimeout(TIME_OUT); byte[] fileBytes = Files.readAllBytes(Paths.get(FILE)); String fileContent = new String(fileBytes, "UTF-8"); // request headers (add all headers needed) con.setRequestMethod("POST"); con.setRequestProperty("Content-length", String.valueOf(fileBytes.length)); con.setRequestProperty("Content-Type", "text/xml;charset=UTF-8"); // Send post request con.setDoOutput(true); con.setReadTimeout(TIME_OUT); DataOutputStream outputStream= new DataOutputStream(con.getOutputStream()); outputStream.write(fileContent.getBytes()); int responseCode = con.getResponseCode(); System.out.println("responseCode:" + responseCode); InputStream in = null; if (con.getResponseCode() >= 200 && con.getResponseCode()<= 300 ) { in = con.getInputStream(); } else { in = con.getErrorStream(); } System.out.println("\nSending 'POST' request to URL : " + URL); System.out.println("Response Code : " + responseCode); Scanner s = new Scanner(in).useDelimiter("\\A"); String result = s.hasNext() ? s.next() : ""; System.out.println(result); } }
Change the FILE path to your request file and ejecute the java. In the console you should see a similar response as previously shown by SoapUI.
Notes
-
If you are using HTTPS you may need to add a certificate to your JDK/JRE
-
If your WS is JSON you may need to change the Content-type header.