RESTful web services JAXRS json example using jersey

In this post, we will see RESTful web services JAXRS json example using jersey.

In previous post, we have created a very simple Restful web services(JAXRS) using jersey which returns xml. In this post, we will see Restful web services(JAXRS) using jersey which will return json as response.

Here are steps to create a simple Restful web services(JAXRS)  using jersey which will return json.
1) Create a dynamic web project using maven in eclipse named “JAXRSJsonExample”

2) We need to add jersey jars utility in the classpath.
Jersey internally uses Jackson for Json Handling, so it will be used to marshal pojo objects to JSON.

Now create pom.xml as follows:
pom.xml

Application configuration:

3) create web.xml as below:

Create bean class

4) Create a bean name “Country.java” in org.arpit.java2blog.bean.

Create CountryRestService

5) Create a controller named “CountryRestService.java”

@Path(/your_path_at_class_level) : Sets the path to base URL + /your_path_at_class_level. The base URL is based on your application name, the servlet and the URL pattern from the web.xml” configuration file.

@Path(/your_path_at_method_level): Sets path to base URL + /your_path_at_class_level+ /your_path_at_method_level

@Produces(MediaType.APPLICATION_JSON[, more-types ]): @Produces defines which MIME type is delivered by a method annotated with @GET. In the example text (“text/json”) is produced.

6) It ‘s time to do maven build.

Right click on project -> Run as -> Maven build

7) Provide goals as clean install (given below) and click on run

Run the application

8) Right click on project -> run as -> run on server
Select apache tomcat and click on finish
9) Test your REST service under: “http://localhost:8080/JAXRSJsonExample/rest/countries”.

You will get following output:

10) Now pass country id as parameter to url.
“http://localhost:8080/JAXRSJsonExample/rest/countries/3”.

click to begin
20KB .zip

Project structure:


We are done with Restful web services json example using jersey. If you are still facing any issue, please comment.


import_contacts

You may also like:

Related Posts

  • 04 August

    Spring Restful web services xml example

    Table of ContentsWeb service Tutorial Content:Spring application configuration:Create bean classCreate controllerRun the application This post is in continuation with web service tutorial (Part -10). In this tutorial,we are going to see Spring Restful web services xml example. Web service Tutorial Content: Introduction to web services Web services interview questions SOAP web service introduction RESTful web service […]

  • 26 June

    Difference between SOAP and REST web services in java

    We have already seen SOAP web services and RESTful web services in detail before. In this post, we are going to see differences between SOAP and REST web services. SOAP vs REST web services Parameter SOAP REST Acronym SOAP stands for simple object access protocol REST stands for REpresentational State Transfer Protocol vs Architectural style […]

  • Web services interview questions
    24 June

    Top 20 Web services interview questions

    Web services interview questions are most asked questions if you are applying for software developer role. In this post, we will see multiple web services interview questions. 1. What are web services? Web services are ways of communication between two application over network. It allows you to expose business logic using API. For example: Lets […]

  • Restful web services interview questions
    22 June

    Restful web services interview questions

    Restful web services are very popular now a days because it is very simple to implement and less time consuming. In this post, we are going to see restful web services interview questions with answers. 1. What is REST? REST is an architectural style which was brought in by Roy Fielding in 2000 in his […]

  • 15 April

    Spring Restful web services CRUD example

    Table of ContentsWeb service Tutorial Content:Source code:Maven dependenciesSpring application configuration:Create bean classCreate ControllerCreate Service classRun the applicationGet methodPost methodPut MethodDelete methodProject structure: In this post, we are going see Spring Restful web services CRUD example. Web service Tutorial Content: Introduction to web services Web services interview questions SOAP web service introduction RESTful web service introduction Difference […]

  • 29 March

    RESTful web services JAXRS CRUD example using jersey

    Table of ContentsSource code:Maven dependenciesApplication configuration:Create bean classCreate ControllerCreate Service classRun the applicationPost methodPut Method This post is in continuation with web service tutorial (Part -8). Introduction to web services Web services interview questions SOAP web service introduction RESTful web service introduction Difference between SOAP and REST web services SOAP web service example in java […]

Comments

    1. Yes, It is possible.
      You can go to maven central repository – http://search.maven.org/ – and for each of the listed dependencies search for their associated versions. When you find them in the repository there will be a link to allow you to download the jar file manually.

      Once you have them downloaded locally you just set your classpath to their location when you run the program .
      Let me know if you require more help on it.

  1. Hi Arpit,

    It's very nice tutorial.
    have one doubt how you got src.main.java package, i am able to see only resources package only and bit confused while creating Country class with in that. Please help.

    1. Hi Sruthi,
      Thank you. I have followed Maven folder structure. If you don't find src/main/java , you can create it manually.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.