Spring Restful web services example

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

We have already seen Restful web services tutorial and Spring mvc web example. In this post, we will see combination of both.

Spring have started supporting Restful web services after Spring 3 version. In this post, we will see how to create Restful web services with Spring framework.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 plain text.

1) Create a dynamic web project using maven in eclipse.
2) 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 controller

5) Create a controller named “SpringRestController.java”

@PathVariable: Used to inject values from the URL into a method parameter.This way you inject name in hello 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
9) Test your REST service under: “http://localhost:8080/SpringRestfulWebServicesExample/hello/arpit”.
You will get following output:

Download

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

That’s all about Spring Restful web services example.


import_contacts

You may also like:


import_contacts

You may also like:

Related Posts

  • 04 August

    Spring Restful web services xml example

    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 introduction Difference between SOAP and REST web services SOAP web service example in […]

  • 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

    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 between SOAP and REST web services SOAP web service example in java using eclipse JAX-WS web service eclipse tutorial JAX-WS web service […]

  • 29 March

    RESTful web services JAXRS CRUD example using jersey

    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 using eclipse JAX-WS web service eclipse tutorial JAX-WS web service deployment on tomcatCreate RESTful web […]

Comments

  1. Hi, your post is good, but why only Hello World example as its so simple.
    Post some credential like from front-end application to connect to database and records gets reflected.

    Regards,
    Devesh A

  2. WARNING: No mapping found for HTTP request with URI [/SpringRestfulWebServicesExample/hello/arpit] in DispatcherServlet with name 'springrest'
    I am getting this above warning with Http status 404 Error

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.