This post shows how to consume a SOAP Web Service using a client application in Kotlin. Although we need Java, we code everything in Kotlin.
Create a SOAP Web Service in Kotlin
Before creating a SOAP Web Service in Kotlin, we must make a SOAP Web Service. The Kotlin codes on this post will work with that SOAP Web Service because we will use its WSDL to generate artifacts. Also, we need that service running to access its WSDL.
Generate JAX-WS Portable Artifacts
Before we code in Kotlin, we need to generate some artifacts using the wsimport command. At this point, we do not need to code anything yet. We are merely setting up our IDE for development. Also, the IDE will use the artifacts to run the SOAP client.
The Web Service server runs on localhost:8080. Hence, its WSDL is available via http://localhost:8080/?wsdl. After generating the artifacts, we get a jar file – soap-client.jar. The filename is arbitrary, and we can choose any name we want.
Then, we reference the jar file in our IDE or via the classpath. Referencing the jar file in our IDE enables it to run our Kotlin SOAP Web Service client locally within the IDE.
Next, we attach the jar file to the Kotlin Java runtime.
Once we attached the soap-client.jar file, we can see it in the list of classes under the Kotlin runtime configuration.
The Web Service Client in Kotlin
Lastly, we create the SOAP Web Service client in Kotlin codes. Note that class HiHelloWSService and the interface HiHelloWS come from the jar file we imported into the IDE. Then, our Web Service client in Kotlin will look like the following codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package com.turreta.client import com.turreta.kotlin.soap.HiHelloWS import com.turreta.kotlin.soap.HiHelloWSService fun main(args: Array<String>) { val service = HiHelloWSService() val hiHello: HiHelloWS = service.hiHelloWSPort println(hiHello.sayHello()) } |
We basically just reused the classes and interfaces from the generated jar file. When we run our codes, we get the following output. Please note that the SOAP Web Service must be running before we run the SOAP Web Service client.
With these Kotlin codes as a base example, we can proceed to build real SOAP Web Service clients.