Monday, January 20, 2014

RESTful web services with jetty and jersey

Recently I was thinking if there is a way to start a web server in java just as easy as it is in node.js, if you don't know node.js you can check this link, so in node you just write a few lines and you have a server like this:

I thought that with something like this you can easily test your code without the burden of deploy it, and also saving some resources. 

So I start looking if there is something similar in java and I found several projects to start a web server or a minimal server, but I decided to go with jetty, so I'm going to show you how to run some restful web services using jetty. 

Also I'm going to use jersey which simplifies the development of RESTful Web services and their clients in Java.

So you can download the jars from these projects or configure them through maven to your project to start using them, in these links , you can check the instructions to do it.

So to create a web server with jetty is as easy as the following code:

If you run the code and check the http://localhost:8080/hello url you can see that your servlet is running, so basically in the code you create a Server object and set the port the Servlets and the servlets to run and that's it.

With jetty you can do much more than this, but for the purpose of this blog I will no go further, you can check the jetty documentation.

The following would be to create and deploy the RESTful web services, now we will use jersey to this.

The first thing to do is to create the services that we will deploy, for the example I will create two services one using JSON and other using XML, I will create two objects used to transfer the data.

In the classes above you can see two classes Employee with a nested class Address and Student, the only thing to notice from these classes are the annotations @XmlRootElement and @XmlAttribute, these annotations are used to do the parsing from object to the protocol used (XML, JSON) and from protocol to object.

The following would be to create the classes for the services.

The classes above use the standard Java EE specification for RESTful web services they use the @Path annotation at the top level to set the service path and each method use the @GET, @POST annotations to describe the type of service and the @Produces annotation to set the type of protocol to use.

The method getStudent of the class XMLStudentSvc has also the @Path("/student/{name}")  this specifies a path for this method, also note that it receives a parameter named "name" through the path and this parameter will be mapped to the parameter of the method.
For more about how define RESTful web services check the specification

Another thing to notice is that both classes are in a package called "rest", so the following to do would be to deploy these services in jetty as long as the jersey configuration.

This is the class for the server:

See in the class above all what it needs to configure jersey, a ServletHolder object is created to set the parameters, note that is indicated the package where the services are located and jersey will automatically deploy them. The ServletHolder object is passed to a ServletContextHandler object, and that's it with this the services should be up and running.

The last thing to do is to create a client for our services, for the client I will use jersey to help.

This is the client for the JSON service:

The client will call two methods, the first will be to the GET method and it uses the url with the path specified for this method ("http://localhost:9999/employee/getEmployee"), this method returns a response in JSON, then this response is unmarshal to a object with the call to "response.getEntity(Employee.class)".

If the JSON response is needed instead of the actual object all what is need to do is change the type to String in the unmarshal "response.getEntity(Employee.class)".

The other method specified in this client is the POST method this call doesn't return a thing,  it uses the url with the path specified for this method ("http://localhost:9999/employee/postEmployee") and it sends back the same object the client is receiving, you should see an output in the server since the service method is printing the object it is receiving.

This is the client for the XML service:

It is almost the same as the JSON client, the only difference is that a parameter is include in the url "http://localhost:9999/xmlServices/student/James", because the service is expecting it.

After running these examples I notice how easy and fast is to run the services and the few resources that required, I'm using at most 30Mb of memory.


  1. Hi, Your posts really helped me a lot in learning new things
    Specially the node.js, I was not aware of this....

  2. In case someone arriving here is using Jersey 2.6 instead of 1.x which is used in the above examples, here is the Servlet Handler changes. Note that Moxy is now the default JSON processor and is loaded dynamically. No need to specify the POJO init parameter. Just make sure and its dependencies are in the classpath and it will be loaded and used.

    ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");

  3. I follow the example and got:
    Exception in thread "main" java.lang.SecurityException: class "javax.servlet.ServletRegistration$Dynamic"'s signer information does not match signer information of other classes in the same package

    1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer Node js Training in Chennai . learn from or Javascript Online Training from India. Nowadays JavaScript has tons of job opportunities on various vertical industry. JavaScript Training in Chennai

  4. I built the restful web service based on this tutorial and I found it very easy to use and understand.

    I had some problems migrating the tutorial from Jersey 1.x to 2.x but finally I got the web server up and running. Now, the problem is that it does not respond to requests from client (or browser). Does anyone know why?

    Here is my code:

    Provider package:


    public class XMLProjectService {
    public String getProject()
    return "" + " hello world " ;
    Jetty embedded server:

    public Object execute(ExecutionEvent event) throws ExecutionException {
    Server server = new Server(8080);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    ServletHolder sh = new ServletHolder(new ServletContainer());
    sh.setInitParameter(ServerProperties.PROVIDER_PACKAGES, "");
    context.addServlet(sh, "/*");

    try {
    } catch (Exception e) {
    System.out.println("Unable to start jetty web server");
    return null;

    public class Test {
    public static void main(String[] args) {
    Client client = ClientBuilder.newClient();
    WebTarget target ="xmlServices");
    private static URI getBaseURI() {
    return UriBuilder.fromUri("http://localhost:8080/").build();

    The result I get is: InboundJaxrsResponse{ClientResponse{method=GET, uri=http://localhost:8080/xmlServices, status=404, reason=Not Found}}

  5. you didn't setinitParameteres properly as what article mentioned :-

    sh.setInitParameter("", "com.sun.jersey.api.core.PackagesResourceConfig");
    sh.setInitParameter("", "rest");//Set the package where the services reside
    sh.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");

  6. Thanks for informative post which is very useful. I appreciate the blog author and like the articles of these blog.
    Web Development Company in Indore

    1. An important building block of SEO is to develop your website which can be easily understandable for both Online Visitors (most important) and search engine robots. In the current digital age, seo is important for your online success means to generate leads.

      SEO Services in IndiaSEO Company in India SEO Company in India SEO Services in India

      SEO Company in India SEO Services in India

  7. Nice post. Gave me inspiration to write a few on my own since newer version don't quite work with this setup:

  8. Hi,

    I have tried to run the server and then access the link http://localhost:9999/employee/getEmployee.

    but it throws below error.

    2015-06-16 07:19:54.879:INFO:oejs.Server:jetty-8.1.14.v20131031
    2015-06-16 07:19:54.983:INFO:oejs.AbstractConnector:Started SelectChannelConnector@
    Jun 16, 2015 7:20:02 AM com.sun.jersey.api.core.PackagesResourceConfig init
    INFO: Scanning for root resource and provider classes in the packages:
    2015-06-16 07:20:02.744:WARN:/:unavailable

    Any Hep..!!

  9. Hi


    i see your post . i am very interested your post so i am very happy .
    website designing company in india

  10. very informative post, Loved reading this post. Hostgator Review

  11. Thanks for information on web services. for more information SEO Services Company in Chandigarh


  12. Its a very good post, you said many useful information here .
    java servlet programming
    hardware and networking course

  13. This is nice information,, Thanks for sharing this information,, Its very useful to me
    mobile app development

  14. How did you get javax.servlet? I'm struggling to work it out, and a lot of answers online seem to boil down to 'use Eclipse EE', which is, of course, no use to me.

  15. Nice Website...
    Hey JOIN now and Increase Facebook Likes your profile and websites.
    Increase Facebook Likes and check your website worth worth my websites
    its may be very beneficial for you also really


  17. Thanks for sharing excellent information. Your site is very cool . I am impressed by the details that you have on this site.Website Designing Bangalore | Web Design Company Bangalore

  18. Thanks for this article, very useful. I have managed to update the jersey examples to jersey 2 and get a response from the server, but I still have 2 problems:
    a) how can I inject dependencies into my jersey resources? they get instanciated with the default ctor but in my case I need to pass in a dao object, so I get a NPE when I do a request
    b) how can I make jersey's logging less verbose? Ideally I want to send it through java.util.logging but I've tried adding and also in the code with Log.setLog(...) but nothing seems to work. thanks,

  19. Thank you for taking the time to this information very useful!
    SEO services pakistan

  20. Hi,
    I used the above examples to get example of JSON and XML Clients. Had a bit of trouble with the JSON client but eventually solved it by including jackson jar. In this case I don't think the com.sun.jersey.api.json.POJOMappingFeature needs to be set in the server code.
    I am using jackson-all-1.9.0.jar, jersey-bundle-1.19.jar, servlet-api-3.0.jar and jetty-8.1.2.

    I will probably move on now and try and get a similar example working with the latest version of Jersey.

  21. Nice Information you have written here. Really Great Stuff. I keep it bookmark for our future purpose.

    We are also Web development Company in India who provide the services in Android App Development in Nagpur , SEO Company in Nagpur , Ecommerce Website Development in Nagpur. Visit Us today

    AceZed IT Solution

  22. Nice Information you have written here. Really Great Stuff. I keep it bookmark for our future purpose. Our Best Presence in Content Marketing Find our Content Writing Services in Nagpur & Content Writing Company in Nagpur .
    AceZed IT Solution

  23. It will very helpful as user point of view. Please keep sharing for the beneficial knowledge of users.

    Web Design services in Lucknow

  24. Accuratesolutionltd führt mobile app Entwicklungsunternehmen. Wir entwickeln Apps für Handys, Tablets , iPads und iPhones . Dies ist aufgrund der zunehmenden Nutzung von Internet und Mobilgeräte in Deutschland .

  25. Grateful to check out your website, I seem to be ahead to more excellent content and I believe we all really like to thank for so many excellent content, weblog to discuss with us Nimble Social CRM

  26. Nice post, I bookmark your blog because I found very good information on your blog, Thanks for sharing more information Dynamic Web Development Services.

  27. Hi, Am having this error.

    "java.lang.UnsupportedClassVersionError: org/eclipse/jetty/server/HandlerContainer : Unsupported major.minor version 52.0"

    Please can someone tell which JDK to use.


  28. Nice post, I bookmark your blog because I found very good information on your blog, Thanks for sharing more information Dynamic Web Development Company.

  29. I just like the title of your post code like a boss. Logo Designs Company

  30. A simple question, What is a website? In its bare form a website is a single domain that consists of different web pages. We should all know that by now, but surprisingly what we don’t all know, is the benefits a website can provide for your business and its shocking to witness how many business don’t actually have a website or online presence!
    A1 Digital Solutions

  31. If quality web design company means,it should providing a quality services in all the development.

    Web Design Company in Delhi | Website Designing Company in Delhi

  32. The blog was absolutely fantastic! Lot of great information which can be helpful in some or the other way. Keep updating the blog, looking forward for more contents...Great job, keep it up..
    Web Development Bangalore | Web Designing Company Bangalore

  33. Programming is combination of intelligent and creative work. Programmers can do anything with code. The entire Programming tutorials that you mention here on this blog are awesome. Beginners Heap also provides latest tutorials of Programming from beginning to advance level.
    Be with us to learn programming in new and creative way.

  34. Hi, is it possible to obtain the pom.xml ?

    So i have a strange error

    2016-08-09 20:08:15.593:INFO::main: Logging initialized @214ms
    2016-08-09 20:08:15.780:INFO:oejs.Server:main: jetty-9.3.11.v20160721
    2016-08-09 20:08:15.852:INFO:oejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@5594a1b5{/,null,AVAILABLE}
    2016-08-09 20:08:15.877:INFO:oejs.AbstractConnector:main: Started ServerConnector@2812cbfa{HTTP/1.1,[http/1.1]}{}
    2016-08-09 20:08:15.877:INFO:oejs.Server:main: Started @502ms
    2016-08-09 20:08:22.544:WARN:oejs.ServletHandler:qtp1469821799-17: Error for /employee/getEmployee
    java.lang.NoSuchMethodError: com.sun.jersey.core.reflection.ReflectionHelper.classForNameWithExceptionPEA(Ljava/lang/String;)Ljava/security/PrivilegedExceptionAction;
    at com.sun.jersey.spi.container.servlet.WebComponent.createResourceConfig(
    at com.sun.jersey.spi.container.servlet.WebComponent.createResourceConfig(
    at com.sun.jersey.spi.container.servlet.WebComponent.init(
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(
    at javax.servlet.GenericServlet.init(
    at org.eclipse.jetty.servlet.ServletHolder.initServlet(
    at org.eclipse.jetty.servlet.ServletHolder.getServlet(
    at org.eclipse.jetty.servlet.ServletHolder.ensureInstance(
    at org.eclipse.jetty.servlet.ServletHolder.prepare(
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(
    at org.eclipse.jetty.servlet.ServletHandler.doScope(
    at org.eclipse.jetty.server.session.SessionHandler.doScope(
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(
    at org.eclipse.jetty.server.Server.handle(
    at org.eclipse.jetty.server.HttpChannel.handle(
    at org.eclipse.jetty.server.HttpConnection.onFillable(
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.executeProduceConsume(
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceConsume(
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(
    at org.eclipse.jetty.util.thread.QueuedThreadPool$

    1. [INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ TestRest ---
      [INFO] TestRest:TestRest:jar:1.0-SNAPSHOT
      [INFO] +- javax.servlet:javax.servlet-api:jar:3.1.0:compile
      [INFO] +- org.eclipse.jetty:jetty-server:jar:9.3.11.v20160721:compile
      [INFO] | +- org.eclipse.jetty:jetty-http:jar:9.3.11.v20160721:compile
      [INFO] | | \- org.eclipse.jetty:jetty-util:jar:9.3.11.v20160721:compile
      [INFO] | \- org.eclipse.jetty:jetty-io:jar:9.3.11.v20160721:compile
      [INFO] +- org.eclipse.jetty:jetty-servlet:jar:9.3.11.v20160721:compile
      [INFO] | \- org.eclipse.jetty:jetty-security:jar:9.3.11.v20160721:compile
      [INFO] +- com.sun.jersey:jersey-servlet:jar:1.17.1:compile
      [INFO] | \- com.sun.jersey:jersey-server:jar:1.17.1:compile
      [INFO] | \- asm:asm:jar:3.1:compile
      [INFO] +- org.glassfish.jersey.core:jersey-client:jar:2.23.1:compile
      [INFO] | +-
      [INFO] | +- org.glassfish.jersey.core:jersey-common:jar:2.23.1:compile
      [INFO] | | +- javax.annotation:javax.annotation-api:jar:1.2:compile
      [INFO] | | +- org.glassfish.jersey.bundles.repackaged:jersey-guava:jar:2.23.1:compile
      [INFO] | | \- org.glassfish.hk2:osgi-resource-locator:jar:1.0.1:compile
      [INFO] | +- org.glassfish.hk2:hk2-api:jar:2.4.0-b34:compile
      [INFO] | | +- org.glassfish.hk2:hk2-utils:jar:2.4.0-b34:compile
      [INFO] | | \- org.glassfish.hk2.external:aopalliance-repackaged:jar:2.4.0-b34:compile
      [INFO] | +- org.glassfish.hk2.external:javax.inject:jar:2.4.0-b34:compile
      [INFO] | \- org.glassfish.hk2:hk2-locator:jar:2.4.0-b34:compile
      [INFO] | \- org.javassist:javassist:jar:3.18.1-GA:compile
      [INFO] \- com.sun.jersey:jersey-client:jar:1.17:compile
      [INFO] \- com.sun.jersey:jersey-core:jar:1.17:compile

  35. This comment has been removed by the author.

  36. Great Work. This post is worth everyone’s attention. web design company in chennai

  37. This comment has been removed by the author.

  38. This is a wonderfully written and helpful article, And this was a long guide. So I appreciate you taking the time to check it out!
    Website Design Company Nagpur

  39. Great Work. This post is worth everyone’s attention.

  40. Informative article, just what I was looking for.seo services chennai

  41. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.

    web hosting company in lucknow | Website designing Company in Lucknow | Domain Registration Service in lucknow | it Company in lucknow

  42. Thanks for your post? But can I ask one question: what is the relationship between Jetty and Jersey? Thanks a lot

  43. Can truly relate and retain this outstanding post. Very well written. web design company Chennai

  44. helpdesk management system is a wonderful way to get help for external resources. The team offered by us are amazing in help desk management and offer the best to satisfy the clients.

  45. Great post! I am actually getting ready to across this information, It's very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    Embedded Training in Chennai


  46. Hai Author, Very Good informative blog post,
    Thanks for Sharing

  47. He has done a great job with such a quality write up. Moving on, I trust MakeWhale when it comes to the best 3D printed products.

    printing services in delhi

  48. Very nice tutorial and nice blog about web services...thanks

  49. Nice post. Thanks for sharing this useful post.
    Software Company in Lucknow provide various services like web designing, development, app development, digital marketing service.

  50. Thank you for taking the time and sharing this information with us. It was indeed very helpful and insightful while being straight forward and to the point. | |

  51. Great Article to know whats going on behind the walls in background

    follow my blog


  52. This Article is Very HelpFul For me You Can Also Visit my Website HostOne is a Pakistan based web hosting company, We Offered You 30% Off Web Hosting in Pakistan providing both providing reliable hosting services since 2011
    Best Web Hosting in Pakistan

  53. Best article for me. visit my website
    we provide all digital marketing service include web development, email marketing, SEO service, Social media marketing, SMS marketing, Domain Hosting service in all over pakistan
    digital agencies in karachi

    digital agency in karachi

    digital agency pakistan

    digital marketing agency

    digital marketing agency in karachi

    digital marketing agency in pakistan

  54. Web Design Sydney: It is a great sharing...I am very much pleased with the contents you have mentioned. I wanted to thank you for this great article.Logo Design Sydney, Logo Design in Coimbatore, Brochure Design in Coimbatore

  55. Lovely blog with much more interesting article, I will keep on reading your update. Thanks for the share Ear Plugs for Swimming Ear plugs for Sleeping Custom Ear Plugs

  56. Most of people are using Jersey 2.6 rather than of 1.x but things got weird when it comes to Servlet Handler changes and the default JSON processor is loaded dynamically. Thanks for making everything clear about Servlet handler changes as it's the part where everyone do mistakes.

    Software Development Company In Indore

  57. Web Design Sydney: I really appreciate this post and I like this very much. I am waiting for new post here and Please keep it up in future. .Logo Design Sydney,Logo Design in Coimbatore


  58. Lovely blog with much more interesting article, I will keep on reading your update. Thanks for the share Ear Plugs for Swimming Ear plugs for Sleeping Custom Ear Plugs

  59. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.

    or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

  60. Nice Post and tuitorial. Thanks for sharing this valuable information.
    Servers from SUN Oracle at Rentals

  61. It is really interesting for me to read this article. Thanks for it. I like such topics and everything connected to them.
    Online Marketing Services
    SEO Company Bangalore
    seo pricing packages india