Summary
The objective of this article is to provide a good start on automated generation of SOAP Web Services using MDA tool “AndroMDA”. Here we will create a new web services project, and apply typical development iteration to it.
The sections are classified in a simple manner to provide an easy understanding on the flow:
Generating an AndroMDA Project
One of my previous article talks 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 web service project using AndroMDA.
Web Services Project Generation Specification:
Type of Application : J2EE
Parent Directory : /Users/jeeva/Documents/svn/ (Provide your system path)
First name and Last name : Jeevanandam Madanagopal (Provide your name)
Kind of Modeling Tool : UML2
Project Description : SOAP Web Service AndroMDA
Project Maven Artifact Id : soap-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
- Have a look at AndroMDA generated project in the provided directory
mac-book-pro:soap-webservice jeeva$ ll total 144 drwxr-xr-x 4 jeeva staff 136B Jul 23 18:00 CXF/ drwxr-xr-x 7 jeeva staff 238B Jul 23 18:00 common/ drwxr-xr-x 8 jeeva staff 272B Jul 23 18:00 core/ -rw-r--r-- 1 jeeva staff 2.7K Jul 22 20:06 install.log -rw-r--r-- 1 jeeva staff 2.7K Jul 22 20:06 m2eclipse.bat drwxr-xr-x 14 jeeva staff 476B Jul 23 17:41 mda/ -rw-r--r-- 1 jeeva staff 55K Jul 22 21:06 pom.xml -rw-r--r-- 1 jeeva staff 12K Jul 22 20:06 readme.txt drwxr-xr-x 4 jeeva staff 136B Jul 23 18:01 webservice/ mac-book-pro:soap-webservice jeeva$
UML Modeling using MagicDraw UML
Considering the fact that we are familiar with MagicDraw, I picked up MagicDraw UML 17.0.2 tool to model. Open up the model file in the MagicDraw; as per our article, the location where the model is placed under
{base-directory}/soap-webservice/mda/src/main/uml/soap-webservice.xml
While opening model file, MagicDraw will present you question of two Path Variables
- maven2.repository – value is {path to your local maven repo}
- andromda3.root – value is {path to your local maven repo}
For example: (below path as per my machine)
maven2.repository => /Users/jeeva/.m2/repository andromda3.root => /Users/jeeva/.m2/repository
Now, model a couple of Value Objects, one Sample web service class, few Methods in it. As per below package structure & UML diagram.
One of my article talk about UML Modeling for AndroMDA, you can go through and achieve the modeling comfortably.
Let us move on to the third step after successfully completing our modeling!
Creating ready-to-deployable CXF Web Service Client
We are getting into creation of web service client to consume our generated web service.
- create a directory called
'cxf-client'
inside the'{base-directory}/soap-webservice'
- create a pom.xml file inside
'{base-directory}/soap-webservice/cxf-client'
directory - pom.xml maps the generated WSDL files and XJB Bindings files
- Add
<module>cxf-client</module>
in the master pom for module chain - comment out
<!-- module>CXF</module -->
in the master pom
Note: 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. Reason behind commenting out CXF module is, it may fail some times and cause trouble in the code generation flow.
Editing the andromda.xml configuration to add below element in the web service section and save the file.
<property name="useAttributes">false</property>
Okay, we done with web service client creation module. Let’s move on!
AndroMDA Code Generation & Compilation
Just issue a following maven command for Code generation & compilation.
mac-book-pro:~ jeeva$ cd {base-directory}/soap-webservice mac-book-pro:soap-webservice jeeva$ mvn install
Now, wait for Code generation to get complete :) and you will see below message at the end.
[INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary: [INFO] [INFO] SOAP Web Service AndroMDA ......................... SUCCESS [1.567s] [INFO] SOAP Web Service AndroMDA MDA ..................... SUCCESS [38.396s] [INFO] SOAP Web Service AndroMDA Common .................. SUCCESS [6.333s] [INFO] SOAP Web Service AndroMDA Core Business Tier ...... SUCCESS [5.656s] [INFO] SOAP Web Service AndroMDA Web Services ............ SUCCESS [27.541s] [INFO] cxf-client ........................................ SUCCESS [6.532s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1:26.690s [INFO] Finished at: Mon Jul 23 17:28:01 GMT+05:30 2012 [INFO] Final Memory: 60M/265M [INFO] ------------------------------------------------------------------------
Coding on {Web Service}WSDelegate
Now we will add our custom codes in the generated skeleton. Let us edit SampleWebServiceWSDelegate.java under core module.
Execute below maven commands to get project ready for ecilipe
mac-book-pro:soap-webservice jeeva$ mvn eclipse:eclipse -f common/pom.xml mac-book-pro:soap-webservice jeeva$ mvn eclipse:eclipse -f core/pom.xml
Now maven modules are ready import in eclipse as a project. Import and Add the following code snippets into SampleWebServiceWSDelegate.java respective methods.
getDates
DatesVO rtn = GetDatesImpl.getDatesTransformResponse(); return rtn;
getServiceGreeting
GreetingVO rtn = new GreetingVO(); rtn.setMessage("Welcome to SOAP Web Services Using AndroMDA by www.myjeeva.com"); return rtn;
getStrings
StringsVO rtn = new StringsVO(); rtn.setOptional(optional); rtn.setRequired(required); return rtn;
For each Web Service method AndroMDA generates two method for value transformation purpose, can be customized for specific requirement.
- get{web-service-method-name}TransformInput
- get{web-service-method-name}TransformResponse
Comment out the following ANT call in mda/pom.xml to avoid deletion of Impl files in subsequent code build & generation
<!-- Comment this to stop deleting Implementation code each build --> <!-- ant antfile="${project.basedir}/build.xml"> <target name="cleanImpl"/> </ant -->
Download’s & Generated project from this Article
Now clone it from GitHub or download the Zip version. It include the sample project (will be used in next section) for consuming generated web service.
GitHub (soap-webservice): https://github.com/jeevatkm/generic-repo.git
Download: https://github.com/downloads/jeevatkm/generic-repo/soap-webservice.zip
Consuming Web Service with Sample Project
We will be executing the sample project to consume generated web service. Lets start the webservice producer instance using jetty. We have done small coding, so compile the code base.
Compilation:
mac-book-pro:soap-webservice jeeva$ mvn install
Starting Web Service Instance:
mac-book-pro:soap-webservice jeeva$ cd webservice mac-book-pro:webservice jeeva$ mvn jetty:run
Now web service available at http://localhost:9090/soap-webservice-webservice/services/SampleWebService?wsdl
Executing Sample Project: execute below maven command; then sample project will ready to import eclipse
mac-book-pro:soap-webservice jeeva$ mvn eclipse:eclipse -f sample-project/pom.xml
After import, open ‘SampleProject.java’ file and execute as Java Application. You will get following response in eclipse console window.
[23/07/12 06:00:01:001 GMT+05:30] INFO demo.SampleProject: Web Service Client App Context Successfully [23/07/12 06:00:01:001 GMT+05:30] INFO demo.SampleProject: Invoking getServiceGreeting Method Welcome to SOAP Web Services Using AndroMDA by www.myjeeva.com [23/07/12 06:00:02:002 GMT+05:30] INFO demo.SampleProject: Invoking getStrings Method welcome to Sample Project by www.myjeeva.com