Wednesday Aug 07

Key Concepts

Web Services with Java EE

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

webservices_logo

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

package com.ubiteck.bank.ws;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import com.ubiteck.bank.dto.AccountDTO;
import com.ubiteck.bank.ws.exception.NullIbanException;
@WebService(
        name=IbanServiceImpl.WS_NAME,
        serviceName=IbanServiceImpl.WS_SERVICE_NAME,
        targetNamespace=IbanServiceImpl.WS_NAMESPACE
)
public class IbanServiceImpl 
{
    public final static String WS_NAME="Account";
    public final static String WS_SERVICE_NAME="AccountService";
    public final static String WS_NAMESPACE="http://bank.ubiteck.com";
    @WebMethod 
    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
Service.

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 :

          http://localhost:8080/sample/AccountService?wsdl

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" 
    targetNamespace="http://bank.ubiteck.com" 
    xmlns:ns1="http://ws.bank.ubiteck.com/" 
    xmlns:ns2="http://schemas.xmlsoap.org/soap/http" 
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
    xmlns:tns="http://bank.ubiteck.com" 
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <wsdl:import 
        location="http://localhost:8080/sample/AccountService?wsdl=IbanService.wsdl" 
        namespace="http://ws.bank.ubiteck.com/">
    </wsdl:import>
  <wsdl:binding name="AccountServiceSoapBinding" type="ns1:IbanService">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="resolveIBAN">
      <soap:operation soapAction="" style="document"/>
      <wsdl:input name="resolveIBAN">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="resolveIBANResponse">
        <soap:body use="literal"/>
      </wsdl:output>
      <wsdl:fault name="NullIbanException">
        <soap:fault name="NullIbanException" use="literal"/>
      </wsdl:fault>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="AccountService">
    <wsdl:port binding="tns:AccountServiceSoapBinding" name="AccountPort">
      <soap:address location="http://localhost:8080/sample/AccountService"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

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

TNUQDKPZCMUG

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

t: TNUQDKPZCMUG

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

Java Tutorial on Facebook