RESTful Web Service (JAX-RS) JSON Example Using Jersey

This article describes how to get a JSON response from the RESTful web services using jersey implementation.  Jersey will use Jackson to convert Java objects to/form JSON, but just don’t ask me what is Jackson :-) ,as of now just remember its a high performance JSON processor, Jersey will use this API to the marshaling [converting the objects] process. Check this link if you would like to know more about Jackson.

These steps are mandatory in order to make Jersey to support with JSON mappings.

  • Apart from existing dependencies, add ‘jersey-json.jar‘ to your Maven pom.xml which includes all Jackson and other JSON supporting libraries
  • In web.xml add “com.sun.jersey.api.json.POJOMappingFeature” as “init-param”
  • In the web service class, we need to annotate the method with @Produces(MediaType.APPLICATION_JSON). By doing so we are instructiong the service method that we are expecting the JSON output, thats it jersey will take care rest of the things

Note: In previous examples i used Tomcat 6 and JDK 1.6 but for this [JAX-RS JSON Example] example i have used JDK 1.7.

Required Files

  • pom.xml
  • web.xml




You Might Also Like

  ::. About the Author .::

- Full Stack Java/J2EE & UI Web Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


16 Responses to “RESTful Web Service (JAX-RS) JSON Example Using Jersey”
  1. Rammohan says:

    All the tutorials available in Java4S are very good and real kick start to learn java based technologies.

    I would request Java4S to add Soap based web services tutorials and Spring Security tutorials as well.

  2. Mohammed says:

    Tutorials are very informative and easy to understand.

    I would like to request you to give an example for consuming the JSON object as well. And how to pass the JSON object from the client side(HTML) using javascript or jQuery to web service.

  3. Nagarjuna says:

    Tutorials are easy to understand and the way you explained with examples is damn good…:)
    You did a great job thank you siva teja..

  4. Puja says:

    Can u plz show an example of a project having spring, maven with restful webservice and junit test.

  5. Kirthika says:

    I tried the example you mentioned and I am getting the error as described below :

    HTTP Status 500 – Servlet.init() for servlet jersey-serlvet threw exception

    root cause

    com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.

    I checked in google for this error and the solution which they gave was to have the init param value properly

    Can you please help me to overcome this error.

  6. Vijay says:

    Tutorials are very good.You did Great job.
    Thanks Siva Teja.

  7. Ankita says:

    how can i build the same example in intellij idea.. can anyone help me???

  8. VjKumar says:

    @Kirthika : Please make sure you provided package path properly.


    Hope this helps !

  9. Frank says:

    Excellent tutorial! Simple and to the point. Bravo!!!

  10. lokesh says:

    Servlet jersey-serlvet is not available

  11. Ravi Singh says:

    How to use Rest api for JIRA plugin Gadget,please show one example for it.
    Thanks in advance.

  12. veera says:

    Excellent tutorial. Can you share client program for sending request to service for geting data using @GET annotation. Atleast we need to pass pojo object from client to service.

  13. sachin says:

    sir I am trying to execute this example with same url you are providing but i am getting 404 as the response

  14. lin says:

    This is an excellent tutorial and has helped alot in understanding web services. Although, i did get few errors, i did resolve it myself. Would you be able to do a tutorial for database connectivity(postgresql if possible) in restful webservices

  15. Sujith Babu says:

    Very great examples Sir(Precise & clear)……Thanks a lot Ji :)


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Hibernate Spring Struts
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2018 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.