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. 

Posted in Java Development
7 comments on “Using Tomcat as JAVA REST server
  1. Satish says:

    I get 404- not found when following the steps above

    • Tobias Blickle says:

      Hi Satish,
      can you share the log output of your tomcat during startup?
      This would help identifying the root cause.
      Regards, Tobias

  2. Rick says:

    I have tried multiple times, two versions of tomcat and eclipse with no luck, still a 404.

    • Tobias Blickle says:

      Hi Rick,
      thanks for pointing out. I discovered there was a bug in my web.xml example (fixed now). The parameter-value must be online.blickle.helloworld not online.blickle.helloworld.resource, as the resource class is in the package online.blickle.helloworld. If this does not help, please consider uppercase/lowercase spelling of the url. Depending on how you set up the project, the url might be http://localhost:8080/heating/rest/world.
      Hope this helps!
      Regards, Tobias

  3. Dietrich Christopeit says:

    Perfect example!

    Many thanks!
    This is the one tutorial that worked right from the start.
    I learned a lot.
    Thanks again!

  4. Mark says:

    Hi, can you post the eclipse project? thanks

  5. Hi, so I was trying this on OSX 10.15. Java 8 something. Eclipse Java EE IDE for Web Developers. Version: 2018-09 (4.9.0)

    It seems to start up tomcat fine (there were missing java class errors but I got past those with updating Deployment Assemblies). But I still get a 404 when trying to access the services and it doesn’t seem to be compiling the Java Classes WorldResource, WorldState (at least no .class in the build directories).

1 Pings/Trackbacks for "Using Tomcat as JAVA REST server"

Leave a Reply

Your email address will not be published. Required fields are marked *