REST Web Services using AndroMDA

Summary

REST is about using the principles of the World Wide Web to build applications.  REST stands for REpresentational State Transfer.  REST offers a simple, interoperable, and flexible way of writing web services that can be very different than the RPC mechanisms like CORBA and WS-*; RESTful web services over HTTP tries to leverage HTTP in its entirety using specific architectural principles such as

  • Addressable Resources (Every objects with its own specific URI)
  • Uniform Constrained Interface (GET, POST, PUT & DELETE, etc)
  • Representation Oriented (Referenced object in different representations [i.e] format)
  • Communicate Statelessly (Highly scalable)

If we are provided with a tool to generate such RESTful web services, what more we need in the world (an exaggeration :))

Well the objective of this article is to provide a good start on automated generation of REST Web Services using MDA tool “AndroMDA”.  Here we will create a new REST web services project.

The sections are classified in a simple manner to provide an easy understanding on the flow:

Article and Sample Demo Project covers Web Service and Service layer with following framework:

  • Apache CXF 2.6.2
  • Spring 3.x

Generating an AndroMDA J2EE Project

One of my previous article talked about creating a J2EE project using AndroMDA.  Check out the article (How to create Java/J2EE project using MDA tool – AndroMDA?) to get acquaintance with the steps to create a project.  In a similar way follow the below given specification to create a project – REST web services using AndroMDA.

Web Services Project Generation Specification:

Type of Application    : J2EE

Parent Directory    : /Users/jeeva/Documents/andromda/ (Provide your system path)

First name and Last name    : Jeevanandam Madanagopal (Provide your name)

Kind of Modeling Tool    : UML2

Project Description    : REST Web Services using AndroMDA

Project Maven Artifact Id    : rest-webservice

Project Version    : 1.0-SNAPSHOT

Project Root Package (maven group Id)    : com.myjeeva.andromda.demo

Package Type    : war

Type of transactional/persistence    : spring

Programming Language for Service and DAO    : Java

Database backend for the persistence layer    : oracle

Workflow engine capabilities    : No

Web user interface    : No

Web Service Enabled    : Yes

SOAP stack    : CXF

JAX-WS REST provider/consumer media type    : json

The embedded Jetty web server    : Yes

Following the above response to the questions will end up getting your project generated successfully.

Let’s verify few essential things before we move to the next step UML modeling.

  • Go to your Local Maven Repository and check this path for AndroMDA UML profiles
    • {your-maven-repo-path}/org/andromda/profiles/uml2

      AndroMDA UML2 Profiles

      AndroMDA UML2 Profiles

    • Have a look at AndroMDA generated project in the provided directory


UML Modeling using MagicDraw UML

Considering the fact that we are familiar with MagicDraw, I picked up MagicDraw UML 17.0.1 tool to model.  Open up the model file in the MagicDraw; as per our article, the location where the model is placed under

While opening model file, MagicDraw will present you question of two Path Variables

For example: (below path as per my machine)

Now, model a couple of Value Objects, one REST web service resource class, few Methods in it.


Configuration Setup for REST Web Services

Note: After making changes to config files, don’t forget to save them :)

Open master POM (pom.xml) in your favorite text editor

  • Look for following comment line in master POM, below these comment has an exclusion inside cxf-bundle dependencies.  We have to comment out exclusions to enable REST dependencies -

  • CXF module is used to perform; java2ws portion of the build is only there to verify that the cxf code (jaxws annotations and references) generated by AndroMDA is valid for creating wsdl from the services, the result is not really used anywhere else.  We are commenting out CXF module since we don’t require such verification for JAX-RS.

comment out <!-- module>CXF</module --> in the master POM

Open MDA POM (mda/pom.xml) in your favorite editor

  • comment out below line to stop deletion of *Impl’s during a code generation

Open andromda.xml (mda/src/main/config/andromda.xml) in your favorite editor :

  • Updating property ‘outputEncoding‘ from ‘ISO-8859-1′ to ‘UTF-8′


Modeling REST Web Services – Service, Method, & Parameter

Check out one of my article talk about UML Modeling AndroMDA, to acquire the modeling knowledge.  This article covers the REST Web Service annotations in AndroMDA UML tag details.  As per below package structure & UML diagram.

Package Structure: REST Web Services Using AndroMDA

Package Structure: REST Web Services Using AndroMDA

UML Diagram: REST Web Services using AndroMDA

UML Diagram: REST Web Services using AndroMDA

Let us move on to the third step after successfully completing our modeling!


AndroMDA Code Generation, Compilation, & Build

Just issue the following maven command for Code generation, compilation and Build.

Now, wait for Code generation to get complete  and you will see below message at the end.


JAX-RS Data Bindings – Configuring Jackson JSON provider

@Consumes: The @Consumes annotation is used to specify which MIME media types of representations a resource can accept, or consume, from the client.

@Produces: The @Produces annotation is used to specify the MIME media types or representations a resource can produce and send back to the client.

If @[email protected] is applied at the class level, all the response methods accept the specified MIME types by default. If @[email protected] is applied at the method level, it overrides any @[email protected] annotations applied at the class level.

MIME Media Types:  It represents the data format in which Input (@Consumes) and Output (@Produces).  It can be application/json, application/xml, text/plain, text/html, image/jpeg, image/png, application/octet-stream, application/x-www-form-urlencoded, etc.

For this demo purpose we stick to media type as JSON, hence we are configuring Jackson JSON library as provider.

Register either JacksonJsonProvider:

and add this Maven dependency into webservice/pom.xml:

OR JacksonJaxbJsonProvider (when working with JAXB beans): This article uses below approach

and add these Maven dependency into webservice/pom.xml:

Finally, refer above created bean as jaxrs:provider in the webservice/src/main/webapp/WEB-INF/applicationContext-CXF.xml

So, now we have modified the application context; let’s prevent from deletion during a code generation.

Open MDA build file (mda/build.xml) in your favorite editor,

Look for below line inside target ‘cleanWSDL’ and comment it out


Coding on {Web Service}WSDelegate

Now we will add our custom code snippet in the generated skeleton.  Let us edit SearchServiceWSDelegate.java under core module.

Execute below maven commands to get project ready for eclipse

Now maven modules are ready to import in eclipse as a project, let’s do it.

Open SearchServiceWSDelegate.java and modify the method ‘performSearch‘ & add the following following code snippet:

Let’s build it again to reflect our changes!

you will see following success message!


Download’s & Generated project from this Article

You can clone it from GitHub or download the Zip version. It includes the Demo project (will be used in next section) for running created REST web services.

GitHub (directory ‘rest-webservice’): https://github.com/jeevatkm/generic-repo.git

Download: https://github.com/downloads/jeevatkm/generic-repo/rest-webservice.zip


Running Sample Demo Project of REST Web Services

As you followed the article, now you will be ready to run the REST web services in your machine or alternatively you can download using above GitHub reference and build it.

Let’s execute the below command:

Web service started at http://localhost:9090; Okay hits the following REST URI in your browser (below param value is not used inside the method, it just passed to invoke the method)

You will see following data as response in your browser


Happy Learning!

You have successfully learned the REST Web Services using AndroMDA tool, right out-of-the-box for your project need.  I hope this would be helpful for you!

  • Incredible points. Solid arguments. Keep up the good effort.

    backlink software http://www.gscraper.com/

  • Angelo Fernandes

    Hi Jeeva,
    Followed the instructions as per the article.
    I am getting one error as seen below. Can you give me clues to resolve this error?

    INFO [SearchServiceSEIImpl] SearchServiceSEIImplperformSearch: Returning SearchService.performSearch: com.myjeeva.andromda.demo.vo.SearchResultVO@
    0a2268a[documents=[[email protected][id=MA147LL/A,name=Apple 60 GB iPod with Video Playback Black,inStock=true,price
    399.0,features=[iTunes, Podcasts, Audiobooks, Stores up to 15,000 songs, 25,000 photos, or 150 hours of video, 2.5-inch, 320×240 color TFT LCD disp
    ay with LED backlight, Plays AAC, MP3, WAV, AIFF, Audible, Apple Lossless, H.264 video, Notes, Calendar, Phone book, Hold button, Date display, Pho
    o wallet, Built-in games, JPEG photo playback, Upgradeable firmware, USB 2.0 compatibility, Playback speed control, Rechargeable capability, Batter
    level indication]], [email protected][id=6H500F0,name=Maxtor DiamondMax 11 – hard drive – 500 GB – SATA-300,inStock
    false,price=350.0,features=[SATA 3.0Gb/s, NCQ, 8.5ms seek, 16MB cache]], [email protected][id=IW-02,name=iPod &a
    p; iPod Mini USB 2.0 Cable,inStock=true,price=11.5,features=[car power adapter for iPod, white]]]]
    WARN [JAXRSOutInterceptor] No message body writer has been found for response class SearchResultVO.
    INFO [LoggingOutInterceptor] Outbound Message
    —————————
    ID: 1
    Response-Code: 500
    Content-Type: text/plain
    Headers: {Date=[Wed, 17 Dec 2014 18:05:18 GMT]}
    Payload: No message body writer has been found for response class SearchResultVO.
    ————————————–

  • Angelo Fernandes

    Resolved the issue. I had to revert to CXF 2.6.0 and jettison dependency for the project.