Thursday, April 30, 2015

REST with Jersey and Jetty part 1

REST with Jersey and Jetty part 1

Running Jersy standalone with a Servlet.

A few days ago I started to look into how to run a REST service with an embedded container. Now a friend of Springframework will say, that is easy with Spring, but after looking at a tutorial [http://spring.io/guides/tutorials/bookmarks/] I notice a couple of things. The annotations are not the standard JSR 331/339 ones, and there is a lot of other things from spring framework coming into play. Well, sorry Spring-guys, I don’t want that. I want to go with standard JSR annotations, and no specialities from Spring.

So my ide is to use Jersey which is the reference implementation och JSR 311/339 and run it on a server that can be embedded. What I want to do is to have the following support.

  • Ability to run with an embedded server (my choise: “jetty”)
  • Ability to serve REST calls (my choise: “Jersey”)
  • Ability to communicate wiht JSON. 
After some research I noticed that it is not easy to find any good resource on how to run Jersey with an embedded server. the bes I could find was [http://jlunaquiroga.blogspot.se/2014/01/restful-web-services-with-jetty-and.html] , but I don’t like blogs saying “you only need to include the jars necessary or use maven” but leaving out the information of which the necessary jars are or the specific maven dependencies needed.. the documentation over Jersey is scarce in examples.

I will start this “journey” by building it up step by step. In this part, I will do only what is necessary to start an embedded server.

I used the above blog as a starting point. First step I will show is how to run a servlet in an embedded Jetty.

The first thing to do is to have a dependency to Netty.



The server will be started in a class with a main method, 



There will also be a simple servlet to run in it.



Next post is about how to create a REST service based on Jersey and run it in Jetty.
(Update: changed the version of Jetty dependency, since I in next post use Jersey 2.17 and there was some issues running with Jetty 9.0.2)

No comments:

Post a Comment