Friday, May 1, 2015

REST with Jersey and Jetty part 2

REST with Jersey and Jetty part 2

Getting Jersey into play

In this part, we will introduce Jetty as serving with rest. We will att a class serving a @GET request.

We will need an extra dependency for Jersey to work, and after getting a strange error under startup I ended up changing the dependency on Jetty from the previous post []. I got the following error:

When adding the dependency for Jersey 2.17 after th Jetty dependency. Actually I got it to work by moving the Jetty dependency to after the Jersey dependency. But reading the log you can see that 2.17 if Jersey used Jetty 9.1.1 so, updating Jetty dependency to 9.1.1.v20140108, the order does not matter.
This is the dependencies for this part.

We will also create a new statup-class, It will not be much more code than the one in the previous part, but some extra stuff is needed to get Jersey into play.

The difference here is that we use a ServletHolder with some init parameters to set Jersey up. We also set the path to serve to "/api/*" instead of plain root "/".
That brings us to REST, we need something to show that it works. To do this we have a class under "rest" package called HelloResource.

So now the only thing you have to do is to start upp JerseyServer and surf into [http://localhost:2001/api/hello] and you should see the hello message. And there you have your first little Microservice ;)

In future post will explore how to use more than @GET and also how to add JSON support to this.


  1. Hi! I'm trying to follow your tutorial but I'm stuck in this second step.

    I'm getting this Exception at the line of ServletContextHandler:

    "Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/jetty/server/handler/gzip/GzipHandler
    at app.JerseyServer.main("

    Mi pom.xml dependencies are:



    But I tried copying your exactly versions with the same result. Do you know what I'm missing?

    Thank you in advance.

  2. Ok, it was my fault: I was not updating correctly Maven dependencies after changing to your same versions. But now I'm a litte annoyed by the fact of how easy is to break my new service by using the latest version of dependencies :/

  3. Well, nice to see that someone so quickly try to use it.
    Unfortunately that is the problem with dependencies, they can break easy.

  4. Actually, I agree, It is just that I used the version from the blog I got my starting point. Then when I wrote the second post I saw some problems. Better to change the version it the first post I guess.