Get all updates of Java4s
Follow Us: Email Newsletter

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
  • Customer.java
  • JsonFromRestful.java

pom.xml

 web.xml

 Customer.java

JsonFromRestful.java

Output

You Might Also Like

  ♦ About Author ♦

Java4s_Author
sivateja@java4s.com
Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

13 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

    com.sun.jersey.config.property.packages
    java4s

    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.
    E.g.

    com.sun.jersey.spi.container.servlet.ServletContainer

    com.sun.jersey.config.property.packages
    com.webser

    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

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
Upcoming
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

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