10 - Introduction to EJB and Web Services

Introduction

In simple words, Web Service is a  client and server application that communicate over the web using the hypertext transfer protocol (HTTP).

Web services are mainly used for the purpose of existing application integration, each application provides a set of web services that expose the application features and for integration, each application consumes the features exposed by other applications.

Also web services can be used to integrate different applications developed using different frameworks and technologies (i.e. Java, .NET applications can integrate together using a web service), as web services use standard protocols for communication.

The following chart represents 2 applications developed using 2 different technologies (Java, .Net) and the 2 applications communicate together using web services.

  

 

Currently JEE provides 2 types of web services that can be used to expose application features, Java API for XML Web Service (JAX-WS) and Representational State Transfer (RESTful) Web Services (JAX-RS).

Currently JEE provides 2 types of web services that can be used to expose application features, Java API for XML Web Service (JAX-WS) and Representational State Transfer (RESTful) Web Services (JAX-RS).

JAX-WS vs JAX-RS

JAX-WS uses a SOAP/WSDL style which is useful when there is a need to expose a contract that describes the interface that the web service offers. In JAX-WS, the WSDL – Web Services Definition Language,  is used to provide such interface.

The advantages of JAX-WS are:

  • Much security can be applied to operations invocation
  • Supports asynchronous invocation for web service operations
  • Can maintain a state between client invocations

The disadvantages of JAX-WS are:

  • It is complex to build clients, so tools should be used to build such clients
  • The communication between the web service and the web service clients consume a lot of bandwidth as all transferred data should be represented in XML format.

JAX-RS uses REST and as REST is depends on simple HTTP calls like GET, POST, DELETE…etc., no interface definition is exposed to the service operations.

Advantages of using RESTful web services are:

  • It is so simple to consume and no need to use tools for client building
  • Consumes low bandwidth, can be accessed directly from the client browsers
  • Can be easily accessed from Javascript applications running in client browsers
  • For static responses, caching layers can be used between clients and the web service

Disadvantages of using RESTful web service are

  • It is totally stateless and no state is maintained between operations invocations
  • Less secured than JAX-WS
  • Supports synchronous invocation only for web service exposed operations.

In JEE7, it is allowed to convert an EJB class to be exposed as a Web Service - JAX-WS or JAX-RS. In the next Chapter we will explain both types of web services using an EJB class.

Like us on Facebook