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

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.

Was this post helpful?

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 *