Creating a SOAP Service using JAX-WS Annotations

September 23rd, 2010 by

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:import>
     </xsd:schema>
     </types>
     <message name="getInfo">
     <part name="parameters" element="tns:getInfo"></part>
     </message>
     <message name="getInfoResponse">
     <part name="parameters" element="tns:getInfoResponse"></part>
     </message>
     <message name="doubleString">
     <part name="parameters" element="tns:doubleString"></part>
     </message>
     <message name="doubleStringResponse">
     <part name="parameters" element="tns:doubleStringResponse"></part>
     
     </message>
     <portType name="SampleService">
     <operation name="getInfo">
     <input message="tns:getInfo"></input>
     <output message="tns:getInfoResponse"></output>
     </operation>
     <operation name="doubleString">
     <input message="tns:doubleString"></input>
     <output message="tns:doubleStringResponse"></output>
     </operation>
     </portType>
     <binding name="SampleServicePortBinding" type="tns:SampleService">
     <soap:binding transport="http://schemas.xmlsoap.org/soap/http"
     style="document"></soap:binding>
     <operation name="getInfo">
     <soap:operation soapAction=""></soap:operation>
     <input>
     <soap:body use="literal"></soap:body>
     
     </input>
     <output>
     <soap:body use="literal"></soap:body>
     </output>
     </operation>
     <operation name="doubleString">
     <soap:operation soapAction=""></soap:operation>
     <input>
     <soap:body use="literal"></soap:body>
     </input>
     <output>
     <soap:body use="literal"></soap:body>
     </output>
     </operation>
     </binding>
     <service name="SampleServiceService">
     <port name="SampleServicePort" binding="tns:SampleServicePortBinding">
     
     <soap:address location="http://localhost:8090/soap/sample"></soap:address>
     </port>
     </service>
    </definitions>

Creating a SOAP client

Resources

Tags: , , , , , ,

3 Responses to “Creating a SOAP Service using JAX-WS Annotations”

  1. Albert Says:

    Very helpful tutorials on web services with java… Thank you

  2. Sava Says:

    Just what I needed. Thanks!

  3. Kwaku Says:

    Thanks, very helpful

Search
Tags
Categories