Using Tomcat as JAVA REST server

Of course, no modern web architecture can exist without REST API! This is not just a hype it is really a standard which enables you to development fast and efficient and provides a lot of flexibility. It is also quite easy to set up a porject in Java. Here is how set this up.

Create Project in Eclipse

  • I used Eclipse Kepler pre-packed JEE development environment and setup tomcat as target runtime (e.g. "Apache Tomcat 7.0") as described here.
  • In Eclipse workspace goto "New->Project->Dynamic Web Project" add a project name (e.g. "heating") and select the target runtime "Apache Tomcat 7.0"

  • I used maven for dependency resolution, so I configured the project to be a Maven project by selecting "Configure->Convert to Maven Project" from the context menu of the "Heating" project.
  • As Tomcat is not an JEE container, I need to add some more dependencies to get a good REST support. Edit pom.xml and add the following dependencies. This can be done by either editing the pom.xml or using the GUI in Eclipse

  • Next, edit web.xml to configure the servlet. If you don't see a web.xml file, you can follow these instructions to create one. The parameter jersey.config.server.provider.packages must hold all packages where resource classes exist.
     <servlet-name>Jersey REST Service</servlet-name>
  • Also in web.xml you can configure the URL pattern where your REST API can be acccessed:
     <servlet-name>Jersey REST Service</servlet-name>

Implementing a sample API

As example we implement a "hello world" REST API resource call. In order to keep to the idea of REST our API should represent resources. An the resources are manipulated by the standard HTTP calls GET, PUT, POST, DELETE. 

  • The state of the "world" we be represented in a simple JAVA bean by the attribute "current_time" and "state". This bean just needs to be annotated by @XmlRootElement annotation in order to be serialized:
    package online.blickle.helloworld;
    import java.util.Date;
    import javax.xml.bind.annotation.XmlRootElement;
    public class WorldState {
     private Date current_time;
     private String state;
     public WorldState(String state) {
     this.state = state;
     this.current_time = new Date();
     public Date getCurrent_time() {
     return current_time;
     public String getState() {
     return state;
  • Next we implement the GET command for the resource. Thanks to annotation support this is pretty easily done with @Path, @GET and @Produces annotations:
    package online.blickle.helloworld;
    public class WorldResource {
     public WorldState get() {
     return new WorldState("fine");
  • That's all. Start the server by the "run on server" command from Eclipse and select tomcat as runtime. You can test the API by typing "localhost:8080/Heating/rest/world" in your browser



You see how easy it is to implement your own REST server. The JAVA frameworks reduce the implemenation efforts really and enable you to focus on the business logic. Of course there are many more things to consider, e.g. authorization, error handling, test automation. 

Leave a Reply