Configuring JAX-RS to work on Tomcat (Microservices)

There is currently a buzz around microservices. When these services are served from an application server, JAX-RS is a good option as it has a well supported community and there are lots of books available on the technology.

However, again, Tomcat “out of the box” doesn’t support JAX-RS without a little configuration.

There are two parts needed for Tomcat to use JAX-RS in your application. One is the Jersey library.

Jersey RESTful Web Services framework is an open source, production quality, framework for developing RESTful Web Services in Java that provides support for JAX-RS APIs and serves as a JAX-RS (JSR 311 & JSR 339) Reference Implementation.

Jersey can be used as a library in your web application that is deployed to a Servlet container such as Tomcat. You should add the jersey-server, jersey-servlet and jersey-bundle as dependencies to your project’s pom.xml

<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.18.1</version>
</dependency>
The other part needed is to create a Class in your application that extends the javax.ws.rs.core.Application Class. JAX-RS provides a deployment agnostic abstract class Application for declaring root resource and provider classes, and root resource and provider singleton instances.

This will allow you to configure the path for all the services you create by configuring the javax.ws.rs.ApplicationPath Class as an annotation.

@ApplicationPath(“resources”)
public class JAXRSConfiguration extends Application {

}

That’s it for the configuration. Now all that’s left is to create a Class to act as an API for your service.

@Path(“apathtouse”)
public class SomeWebService {

@GET
public String getSomething() {
return “Hello world!”;
}

}

Then it’s just a simple case of using it, http://localhost:8080/MyApp/resources/apathtouse

As you can see from the example above I used GET. However you can use any of the HTTP methods in your API

  • GET – normally used to query the records
  • POST – normally used to create a record
  • PUT – normally used to update a record
  • DELETE – normally used to delete a record

However, the actual implementation of your methods are completely up to you. You could use the DELETE method to create and the GET method to delete. I would not recommend that though…

Again, I hope this helps someone and I look forward to your comments.

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s