Creating a Maven Spring-REST project

Open Eclipse. Make sure it is a recent version with m2e integration. I am using Kepler version of Eclipse.
Now go to File > New > Maven Project.
rest1

Click Next. Select maven-archetype-webapp.
rest2
Click Next. Enter GroupID (group ID is the name that refers to a group of projects. It should be unique. A name like ‘com.companyname.yourname’ would be suffice) and Artifact ID (Artifact ID is the project name. It should be unique within a groupID).

There can be several projects under the same GroupID, but ArtifactID should be different for each project. I have given GroupID as ‘me.sinu.jugaad’ and ArtifactID as ‘rest’.
rest3
Click Finish to create the project.

The webapp archetype would have created the necessary folder structure and a web.xml for you. This web.xml might be for older Servlets without support for Servlet v.3, it doesn’t matter as we’ll overwrite it later.
You might examine pom.xml now, it may contain a single dependency of jUnit(even if you won’t find it its fine πŸ™‚ )

Now lets examine the dependencies we might require for our simple project:

  • We are building REST services, so we need REST api (javax.ws.rs-api) and an implentation for it. We’ll use Apache CXF implementation (cxf-bundle-jaxrs).
  • We’ll use Spring to wire the beans and for dependency injection. So we need a bunch of Spring libraries (spring-web and spring-context should be enough for our skeleton project. spring-context is needed for ContextLoaderListener which loads the beans by scanning some Spring context xml configuration files)
  • We’ll use a JSON provider which helps in converting object to/from JSON. It is not needed for this skeleton project, but its usually handy in almost all REST projects. So we’ll add one such provider (jackson-jaxrs-json-provider) just in case we need it in future.

Here is the pom.xml:

Now create a java folder under src/main. Create a package (me.sinu.jugaad.rest) and a class file inside it(HelloWorld.java).
We’ll just tell a simple “Hello REST!” from our REST service. Here is the class:

Now we’ll create a Spring configuration file under WEB-INF folder, say rest-context.xml:

In this we just defined our helloWorld bean and hooked it up with our JAXRS servlet. We also provided our JSON provider to it to automatically do any marshalling/unmarshalling. You may provide your own providers, which will be necessary as the app grows. Providers can contain Filters or other interceptors.

Now lets define our webapp’s web.xml:

We specify ‘contextConfigLocation‘ for the Spring’s ‘ContextLoaderListener‘ to pick it up as the webapp is loaded. The contextConfigLocation can be given as exact file names or using wild cards.
Since we are using CXF implementation, a CXF servlet is used. The address for our servlet is “/services” and that for our ‘helloREST‘ JAX RS server is “/greet“.
So “WAR_FILE/services/greet” will take us to the JAXRS server. Our simple GET function is annotated with @Path(“/hello”), so to access this we should give as follows: “WAR_FILE/services/greet/hello“.
Now run the project – right-click the project > Run As > Run on server. You might choose a server like Tomcat server v.7. Now in a browser go to “http://localhost:8080/rest/services/greet/hello” (here ‘rest‘ is the WAR file name).

If you see “Hello REST!” everything is fine, else, Dr.Watson, we have a problem πŸ˜‰

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s