Wednesday Aug 07

Key Concepts

Web Services with Java EE

Tuesday, 02 April 2013 08:14
AddThis Social Bookmark Button


Web Services with Java EE

With this article we start a new article series about Web Services from the basics to the advanced techniques to test web services with Mock objects and frameworks like arquillian.

This sample service will serve as a base to delve into all the aspect of Web Services in the Java world. The service used as a sample is decoding an IBAN (International Banking Account Number) into all its constituent parts. The resulting object in an Account data object containing the decoded information contained in the IBAN.

In the following example we will use the built-in JAX-WS api contained in the JDK. JAX-WS means Java Api for XML Web Services.JAX-WS replaced the JAX-RPC api in Java EE5.

Iban decoder service

Let's start by declaring our Web service using the JAX-WS api.

Sample Web Service using Jax-WS - Annotations explained

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
public class IbanServiceImpl 
    public final static String WS_NAME="Account";
    public final static String WS_SERVICE_NAME="AccountService";
    public final static String WS_NAMESPACE="";
    public AccountDTO resolveIBAN(@WebParam (name="iban") String iban)  throws NullIbanException{
        ..... do the job and return an AccountDTO

In line 7 we are declaring the class as a Web service using the java.jws.WebService annotation. The purpose of this annotation is to mark this class as implementing a Web Service, or a java interface as defining a Web service interface.

Line 8, we are defining the name of the Web Service used as the name of the wsdl:portType according to WSDL 1.1.

Line 9 is defining the service name of the Web Service used as the name of the wsdl:service when mapped to WSDL 1.1

Line 10 we are defining the XML namespace used for the WSDL and XML elements generated from this Web

To make it available at runtime without processing annotations we are using static string.

Line 17 we are declaring the following method as a WebMethod using the java.jws.WebMethod annotation.

@WebMethod annotation customizes a method that is exposed as a Web Service operation.

Line 18 with the @WebParam annotation we are customizing the mapping of the iban parameter to a Web Service message part and XML element.

Our new service deployed

We will cover the deployment of our service in another article in this serie. In this chapter we will simply describe the artifacts available.

We have deployed our Web Service in a Web application using the context root /sample. To get a description of our Web Services available we are calling the following url :


As a result we have received an XMl based interface description called WDSL description. It provides a machine-readable description of how the service can be called, what parameters it expects, and what data structures it returns. It thus serves a roughly similar purpose as a method signature in a programming language.

Web Service tutorial with Jax-WS WSDL explained

<?xml version='1.0' encoding='UTF-8'?>
<wsdl:definitions name="AccountService" 
  <wsdl:binding name="AccountServiceSoapBinding" type="ns1:IbanService">
    <soap:binding style="document" transport=""/>
    <wsdl:operation name="resolveIBAN">
      <soap:operation soapAction="" style="document"/>
      <wsdl:input name="resolveIBAN">
        <soap:body use="literal"/>
      <wsdl:output name="resolveIBANResponse">
        <soap:body use="literal"/>
      <wsdl:fault name="NullIbanException">
        <soap:fault name="NullIbanException" use="literal"/>
  <wsdl:service name="AccountService">
    <wsdl:port binding="tns:AccountServiceSoapBinding" name="AccountPort">
      <soap:address location="http://localhost:8080/sample/AccountService"/>

This is the end of the first article dedicated to Web services development using Java and JAX-WS.


If you have any remark or questions feel free to put a comment.If you enjoyed this tutorial and want to promote it don't hesitate to click on


Tags: java, import, object, jax-ws, account, service, services, iban, contained

Java Tutorial on Facebook