Spring Restful web services json example


In this post, we will see Spring Restful web services json example.

In previous post, we have created a very simple Spring Restful web services  which returns plain text. In this post, we will see Spring Restful web services which will return json as example. If you want complete integration with hibernate and mysql, you can go through Spring Restful hibernate mysql example.

Here are steps to create a simple Spring Restful web services which will return json.
1) Create a dynamic web project using maven in eclipse.

2) We need to add Jackson json utility in the classpath.

Spring will load Jackson2JsonMessageConverter into its application context automatically. Whenever you request resource as json with accept headers=”Accept=application/json”, then Jackson2JsonMessageConverter comes into picture and convert resource to json format.

Now change pom.xml as follows:

Spring application configuration:

3) Change web.xml as below:

4) create a xml file named springrest-servlet.xml in /WEB-INF/ folder.
Please change context:component-scan if you want to use different package for spring to search for controller.Please refer to spring mvc hello world example for more understanding.

Create bean class

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

Create controller

6) Create a controller named “CountryController.java”

@PathVariable: Used to inject values from the URL into a method parameter.This way you inject id in getCountryById method.

We are not providing any view information in springrest-servlet.xml as we do in Spring MVC. If we need to directly get resource from controller, we need to return @ResponseBody as per Spring 3 but with Spring 4, we can use @RestController for that.
In spring 4.0, we can use @RestController which is combination of @Controller + @ResponseBody.

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

When you run the application, you might get this kind of warning

Please ignore above warning. When you start application, you have below URL if you have not provided start page:

As we have used DispatcherServlet in web.xml, this request goes to spring DispatcherServlet and it did not find corresponding mapping in controller , hence you get that warning.

9) Test your REST service under: “http://localhost:8080/SpringRestfulWebServicesWithJSONExample/countries”.

You will get following output:

10) Now pass country id as parameter to url.

click to begin
20KB .zip
We are done with Spring Restful web services Json example. If you are still facing any issue, please comment.

That’s all about Spring Restful web services json example.


Related posts


  1. Sarjaycs October 27, 2015
  2. Damian November 11, 2015
    • Noah January 29, 2016
  3. saurabh February 1, 2016
    • arpitmandliya February 1, 2016
  4. saurabh February 2, 2016
    • arpitmandliya February 2, 2016
  5. Brian February 29, 2016
  6. Brian March 1, 2016
  7. Archit March 18, 2016
  8. Ghana April 15, 2016
  9. Yuri September 10, 2016
  10. ameen mohammad September 29, 2016
  11. Ankit joshi January 9, 2017
    • kiran March 6, 2018
  12. jagan April 23, 2017
  13. arjun October 11, 2017
  14. hitesh marathe December 16, 2019

Add Comment