It is possible to create SOAP webservices with only a few lines of code using the JAX-WS annotations. In a productivity environment you might prefer using contract-first instead of code-first to create your webservice but for now we’re going to use the fast method and that means code-first and annotations olé!
Creating the SOAP Service
-
Create a class SampleService with two public methods
-
Annotate this class with @WebService (javax.jws.WebService) – now all public methods of this class are exported for our SOAP service
-
To change the name of an exported method, annotate the method with @WebMethod(operationName = “theDesiredName”) (javax.jws.WebMethod)
-
Finally the service class could look like this
package com.hascode.tutorial.soap; import javax.jws.WebMethod; import javax.jws.WebService; @WebService public class SampleService { @WebMethod(operationName = "getInfo") public String getInformation() { return "hasCode.com"; } public String doubleString(String inString) { return inString + inString; } }
Running the service
-
Create a Main class to create the SOAP endpoint and start the server – javax.xml.ws.Endpoint is a help here :)
package com.hascode.tutorial.soap; import javax.xml.ws.Endpoint; public class Main { public static void main(String[] args) { Endpoint.publish("http://localhost:8090/soap/sample", new SampleService()); } }
A look at the WSDL
-
Running the Main class it’s now possible to take a look at the generated WSDL – it should be available at this URL: http://localhost:8090/soap/sample?WSDL
-
The generated WSDL could look like this
<?xml version="1.0" encoding="UTF-8"?> <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --> <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --> <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://soap.tutorial.hascode.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://soap.tutorial.hascode.com/" name="SampleServiceService"> <types> <xsd:schema> <xsd:import namespace="http://soap.tutorial.hascode.com/" schemaLocation="http://localhost:8090/soap/sample?xsd=1"/> </xsd:schema> </types> <message name="getInfo"> <part name="parameters" element="tns:getInfo"/> </message> <message name="getInfoResponse"> <part name="parameters" element="tns:getInfoResponse"/> </message> <message name="doubleString"> <part name="parameters" element="tns:doubleString"/> </message> <message name="doubleStringResponse"> <part name="parameters" element="tns:doubleStringResponse"/> </message> <portType name="SampleService"> <operation name="getInfo"> <input message="tns:getInfo"/> <output message="tns:getInfoResponse"/> </operation> <operation name="doubleString"> <input message="tns:doubleString"/> <output message="tns:doubleStringResponse"/> </operation> </portType> <binding name="SampleServicePortBinding" type="tns:SampleService"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> <operation name="getInfo"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> <operation name="doubleString"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding> <service name="SampleServiceService"> <port name="SampleServicePort" binding="tns:SampleServicePortBinding"> <soap:address location="http://localhost:8090/soap/sample"/> </port> </service> </definitions>
Creating a SOAP client
-
Follow the steps in my article Create a SOAP client using the JAX-WS Maven Plugin :P