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 "";
     public String doubleString(String inString) {
     return inString + inString;

Running the service

  • Create a Main class to create the SOAP endpoint and start the server – is a help here :)
    package com.hascode.tutorial.soap;
    public class Main {
     public static void main(String[] args) {
     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
     RI's version is JAX-WS RI 2.1.6 in JDK 6. --><!-- Generated by JAX-WS RI at
     RI's version is JAX-WS RI 2.1.6 in JDK 6. -->
    <definitions xmlns:soap=""
     xmlns:tns="" xmlns:xsd=""
     xmlns="" targetNamespace=""
     <xsd:import namespace=""
     <message name="getInfo">
     <part name="parameters" element="tns:getInfo"></part>
     <message name="getInfoResponse">
     <part name="parameters" element="tns:getInfoResponse"></part>
     <message name="doubleString">
     <part name="parameters" element="tns:doubleString"></part>
     <message name="doubleStringResponse">
     <part name="parameters" element="tns:doubleStringResponse"></part>
     <portType name="SampleService">
     <operation name="getInfo">
     <input message="tns:getInfo"></input>
     <output message="tns:getInfoResponse"></output>
     <operation name="doubleString">
     <input message="tns:doubleString"></input>
     <output message="tns:doubleStringResponse"></output>
     <binding name="SampleServicePortBinding" type="tns:SampleService">
     <soap:binding transport=""
     <operation name="getInfo">
     <soap:operation soapAction=""></soap:operation>
     <soap:body use="literal"></soap:body>
     <soap:body use="literal"></soap:body>
     <operation name="doubleString">
     <soap:operation soapAction=""></soap:operation>
     <soap:body use="literal"></soap:body>
     <soap:body use="literal"></soap:body>
     <service name="SampleServiceService">
     <port name="SampleServicePort" binding="tns:SampleServicePortBinding">
     <soap:address location="http://localhost:8090/soap/sample"></soap:address>

Creating a SOAP client


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

Leave a Reply

Please leave these two fields as-is:

Protected by Invisible Defender. Showed 403 to 113,310 bad guys.