In this tutorial we will show how to create a RESTful webservice that processes molecules sent to it from clients and returns results back the clients. The MySCServer is a standard Java EE web application that can be deployed into any servlet container like Apache Tomcat 6.x, which we will use. We will use again NetBeans IDE for developing the webservice and deploying it to Tomcat webserver.
This tutorial assumes that you have NetBeans IDE 6.9.1 installed on your computer as well as Apache Tomcat 6.x webserver, which is bundled with the NetBeans IDE installation. If you followed IJC Plugin: Quick Start tutorial you should have both NetBeans IDE and Apache Tomcat already installed.
Contents:
MySCServer
Web Application project
Let's start with creating the MySCServer
project in NetBeans
IDE. It's an ordinary java web application project that we will use
for implementing and deploying a web service.
'File' -> 'New Project...'
.
This will start the 'New Project'
wizard. As a project type
choose 'Web Application'
in the category 'Java Web'
and click 'Next'
button.
Create server Web Application - choose 'Web Application' project type in 'Java Web' category
'MySCServer'
and click 'Next'
button.
Create server Web Application - enter the project name and location
'Add...'
button.'Java EE'
version should be set to 'Java EE 5'
and the 'Context Path'
to /MySCServer
.
Create server Web Application - selecting the web server
'Finish'
at this panel.
Create server Web Application - do not add any webapp development frameworks
MySCServer
project, which should
look like the one on the screenshot below.
MySCServer Web Application project in IDE's 'Projects' explorer
As a next step we will setup libraries needed by the webservice. This is going to be slightly different from what you are used to when setting up IJC plugin libraries. But we are not developing an IJC plugin here!
The webservice needs two libraries - it needs parts of MarvinBeans library
from ChemAxon to perform operations on the chemical structures sent by
webservice clients and it also needs Jersey Multipart
extension in order to receive and send complex data.
Both libraries can either be downloaded from the internet or directly from this tutorial. When downloading from the internet, please, go to ChemAxon's download page and download Marvin Beans for Java Developers. Jersey Multipart is comprised from two jars that can be downloaded from these links - jersey-multipart-1.1.5.1.jar and mimepull-1.4.jar. For your convenience the jars from both libraries were packaged to a single zip file, which you can download from here.
MySCServer
project. First create folders that will store the
library jars.'Window' -> 'Files'
,
this will take you to the 'Files'
explorer where you can see
directly the files and folders comprising MySCServer project.MySCServer
folder and
choose 'New' -> 'Folder...'
. This will start the 'New Folder'
wizard, which we will use for creating lib/MarvinBeans
folders
as it is shown on the picture below.
'New Folder' wizard - creating lib/MarvinBeans
folder
lib
folder's node, choose 'New' -> 'Folder...'
and create jersey-multipart
subfolder.
lib/MarvinBeans
folder: MarvinBeans-concurrent.jar
,
MarvinBeans-diverse-modules.jar
, MarvinBeans-formats.jar
,
MarvinBeans-formats.mdl.jar
, MarvinBeans.jar
,
MarvinBeans-license.jar
, MarvinBeans-plugin.jar
.
lib/jersey-multipart
folder:
jersey-multipart-1.1.5.1.jar
, mimepull-1.4.jar
.The lib
folder structure with library jars
'Projects'
explorer and r-click
the MySCServer
's project node. Navigate to 'Properties'
-> 'Libraries' -> 'Compile'
to see the list of compile-time libraries.
Then click 'Add JAR/Folder'
button. In the 'Add JAR/Folder'
dialog select all MarvinBeans jars first. Make sure that the jars are
added using the 'Relative Path'
and click 'OK'
.
Setting up MySCServer libraries - adding MarvinBeans jars
Setting up MySCServer libraries - adding Jersey Multipart jars
'OK'
.
The libraries are now set up and we can start creating the webservice.
Creating a RESTful webservice in NetBeans IDE is fairly simple process where most of the work is done by the IDE's wizard. We only have to supply a few bits of key information to the wizard, which will then generate all necessary files and even update the webapp project with required libraries, etc.
'Source Packages'
node in 'Projects'
explorer and choose 'New' -> 'Java Package...'
. In the wizard
panel enter org.myorg.myscserver
as the package name and
click 'Finish'
.
New Java Package wizard - creating org.myorg.myscserver
java package
'New' ->
'RESTful Web Services from Patterns...'
or choose 'New' ->
'Other...'
if the 'New'
submenu does not offer the link
to creating RESTful webservice.
New RESTful Web Services from Patterns wizard - choosing the right wizard type
'Simple Root Resource'
webservice
pattern and click 'Next'
.
New RESTful Web Services from Patterns wizard - choosing webservice pattern
'Path'
and 'Class Name'
values. Enter validator
in the 'Path'
text box
and StructureValidatorService
in the 'Class Name'
text box.'Finish'
.
New RESTful Web Services from Patterns wizard - entering 'Path'
and 'Class Name'
values
web.xml
and also add JAX-RS 1.1
library, which is the RESTful webservices API and its reference implementation
in Jersey 1.1 (JAX-RS RI)
library.
New RESTful Web Services from Patterns wizard - updating MySCServer project configuration to support RESTful webservices
MySCServer
project the project's web.xml
configuration file should look
like this.
web.xml configuration file
StructureValidatorService
class in
org.myorg.myscserver
package.
'Projects'
explorer and open the StructureValidatorService
class in the editor. It contains a skeleton of a webservice generated by
the wizard. Replace the generated code with the following:
StructureValidatorService.java source file
StructureValidatorService
class makes use of ErrorBuilder
class. We have to create this class alongside StructureValidatorService
class.
You should now be familiar with NetBeans IDE and be able to do so in several ways.ErrorBuilder
class contain the following code:
ErrorBuilder.java source file
The project should be ready to compile and deploy to the web server
(eg. Apache Tomcat 6.x in our case). You can do so by r-clicking the
project's node in 'Projects'
explorer and choosing 'Clean and Build'
.
In order to run the project choose 'Run'
from the same
popup menu. The IDE will start Apache Tomcat webserver, deploy the project
to it and then use your default web browser to show index.jsp
from the deployed project. This is a simple Hello World
page.
Congratulations, you have successfully created an implementation of a RESTful web service capable of processing chemical structures and sending results back to it clients. In this tutorial you have learned:
For other IJC plugin development related tutorials please see their complete list here.