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:
pom.xml

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

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:
http://localhost:8080/SpringRestfulWebServicesWithJSONExample/ 

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.
“http://localhost:8080/SpringRestfulWebServicesWithJSONExample/country/2”.

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.


import_contacts

You may also like:


import_contacts

You may also like:

Related Posts

  • 29 April

    Spring boot rest example

    In this tutorial, we will see how to create Restful web services using Spring boot. There are not many differences between creating Rest API using Spring Rest JSON which we have already seen earlier. Spring Boot just makes easier to Rest API. You don’t have to provide any additional JSON dependency in the classpath. Spring […]

  • 14 September

    Spring Rest + Spring Security example

    In this post , we are going to apply Spring Security on Spring Rest example. Web services tutorial: 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 using eclipse JAX-WS web service eclipse tutorial JAX-WS […]

  • 19 August

    Spring @RestController example

    In this post,  we are going to see another important annotation called @RestController. It is generally used while working with Spring restful Web services implementation. Spring MVC tutorial: Spring MVC hello world example Spring MVC Hibernate MySQL example Spring MVC interceptor example Spring MVC angularjs example Spring MVC @RequestMapping example Spring Component,Service, Repository and Controller […]

  • 17 August

    Spring Rest Hibernate example

    In previous post, we have already seen Spring Rest crud example . In this post, we will extend same example and integrate it with hibernate and mysql. We will use following annotations for CRUD operation. Method Description Get It is used to read resource Post It is used to create new resource. It is not […]

  • 08 August

    Spring MVC @RequestMapping annotation example

    We have already seen Spring MVC hello world example. @RequestMapping is one of important annotation which you use in Spring MVC. Spring MVC tutorial: Spring MVC hello world example Spring MVC Hibernate MySQL example Spring MVC interceptor example Spring MVC angularjs example Spring MVC @RequestMapping example Spring Component,Service, Repository and Controller example Spring MVC @ModelAttribute annotation […]

  • 06 August

    Spring MVC angularjs example

    n this tutorial, we will see Spring MVC angularjs example. Spring MVC tutorial: Spring MVC hello world example Spring MVC Hibernate MySQL example Spring MVC interceptor example Spring MVC angularjs example Spring MVC @RequestMapping example Spring Component,Service, Repository and Controller example Spring MVC @ModelAttribute annotation example Spring MVC @RestController annotation example Spring MultiActionController Example Spring […]

Comments

  1. Hello, I have one problem with this example…

    lis 11, 2015 3:09:30 PM org.springframework.web.servlet.PageNotFound noHandlerFound
    WARNING: No mapping found for HTTP request with URI [/SpringRestfulWebServicesWithJSONExample/countries] in DispatcherServlet with name 'springrest'

    1. I think you might have copied over the package value of base-package=”org.arpit.java2blog.controller” /> in springrest-servlet.xml. You need to put in your own package name instead… I fell victim to this error as well and it took me a few too many minutes to find the issue…

    1. Are you using maven for running above project? If yes, then maven will automatically fetch required library and will put it in the build path.
      Let me know if you need more help on this. Thanks for asking

  2. hi i am getting this error now:-

    Feb 02, 2016 2:11:24 PM org.springframework.web.servlet.PageNotFound noHandlerFound
    WARNING: No mapping found for HTTP request with URI [/mavenWebApp/] in DispatcherServlet with name 'springrest'

    my app name is mavenWebAppp

    1. please check springrest-servlet.xml in /WEB-INF/ folder once
      you need to change context:component-scan if you want to use different package for spring to search for controller.
      let me know if you need more help on it.

  3. If not working fine, “No mapping found for HTTP request with URI ” remember, RUN THE MAVEN UPDATE BITCH!

    I was with this problem and thus resolved, rs. THANKS alot

  4. Dear Author,

    Impressed your article has worked as one shot operation.
    Just downloaded, Built with Maven, updated Maven, Run on Tomcat Server.
    Perfect example for Rest WebService.

    Thank you so much.

    Regards,
    Ameen Mohammad

  5. ONE problem I encountered was Junit Dependency was not getting resolve so application was not working. may be the version provided in the example is deprecated now. I removed that dependency and it worked.
    one more thing I did wrong was not changes the import package name for bean class in controller class. I used as is given here ” org.arpit.java2blog.bean.Country” which was wrong according to my folder structure. I know this was silly mistake but even the eclipse was not showing any error.

    BTW thanks arpit . good article.

  6. Dear Author,
    I impressed your code, but i am getting small error.
    org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource.

    any help highly appreciated

    Regards

  7. You don’t know how much you do beneficence on me to provide this solution, you know I have been searching this answer for 5 days. thanks a lot

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.