A Simple RESTful Service

I am using jars from the spring-framework-3.0.2.RELEASE and spring-framework-3.0.2.RELEASE-dependencies downloads as you can see from the screenshot for this example of a RESTful service integrated with Spring. Spring 3.0.2 inherently supports REST in its packaged dependencies. However it is interesting to note that after the 3.0.2 release of Spring the corresponding dependency jar packages were not shipped, most probably due to the ease of using a maven project and directly downloading these jars from the maven repository. In addition to these I add the servlet-api jar that takes care that spring is configured correctly by the tomcat container. (I am using tomcat 7)


	REST Service


	<!-- Setup spring to pull in @Controller, @RequestMapping, etc Configuration
		scans specified packages for classes configured as Spring managed beans and
		automatically sets up objects annotated with @Controller, @Service etc. -->
	<!-- TODO -->
	<!-- Configures view for returning JSON to the client -->
	<!-- TODO -->
	<!-- Converts JSON to POJO and vice versa -->

This is the class that exposes the RESTful methods

package com.mattiz.rest.controller;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;
import com.mattiz.rest.service.MattizService;

@RequestMapping(value = "/restservices")
public class RestServiceController {
	private MattizService mattizService;
	private static final String DATA_FIELD = "data";
	private static final String ERROR_FIELD = "error";
	@RequestMapping(value = "/mattiz", method = RequestMethod.GET)
	public ModelAndView getWelcomeString(HttpServletRequest request) {
		String msg = null;
		try {
			msg = mattizService.getWelcomeString();
		} catch (Exception e) {
			return createErrorResponse(String.format(
					"Error getting message. [%1$s]", e.toString()));
		return new ModelAndView(new MappingJacksonJsonView(), DATA_FIELD, msg);
	private ModelAndView createErrorResponse(String sMessage) {
		return new ModelAndView(new MappingJacksonJsonView(), ERROR_FIELD,

Service interface

package com.mattiz.rest.service;
public interface MattizService {
	public String getWelcomeString();

Service implementation

package com.mattiz.rest.service;

import org.springframework.stereotype.Service;

public class MattizServiceImpl implements MattizService {
	public String getWelcomeString() {
		return ("Hallo Mattiz World!");

This exposes the RESTful service by the URL restservices/mattiz

There are multiple REST clients that can be used to test the application including two plugins to the Chrome browser.
Type the following URL, select GET and hit the SEND button to get the response from the RESTful service.


You would see something like this:

{“data”:”Hallo Mattiz World!”}

An example of Apache CXF web service and RESTful service can be downloaded here


REST is an architecture. REST is not restricted to HTTP alone. It can use any protocol that follows the URI scheme. SOAP on the other hand uses HTTP alone. The consumer need not know a lot about the services exposed by the Rest service provider. In case of SOAP any undocumented change on either server or client side will break the communication. i.e. Web Service client and server are more tightly coupled than REST. The soap client requires detailed previous knowledge of the web service provider. However, once the entry point is known in REST, the resources are supposed to return links that the client can follow. Rest has a much simpler request and can use JSON as well as XML. It does not have the soap envelope and the headers and can be used for protocols that don’t support these. It can use JSON as well as XML or CSV.
REST is almost always going to be faster. The main advantage of SOAP is that it provides a mechanism for services to describe themselves to clients, and to advertise their existence.
REST is much more lightweight and can be implemented using almost any tool, leading to lower bandwidth. However, the clients have to know what to send and what to expect.
So the soap web service client can be thin – service provider has lot of logic and data access and does lot of work while in RESTful web service, the provider is very thin – just provides resources (e.g. data). In general, When you’re publishing an API to the outside world that is either complex or likely to change, SOAP will be more useful. Other than that, REST is usually the better option.

Common HTTP methods for REST

POST – create
GET – read
PUT – update
PATCH – update
DELETE – delete
HEAD – to find what GET will return
Headers provide information about the type of request and the resource requested/ created.

About cuppajavamattiz
Matty Jacob - Avid technical blogger with interests in J2EE, Web Application Servers, Web frameworks, Open source libraries, Relational Databases, Web Services, Source control repositories, ETL, IDE Tools and related technologies.

Comments are closed.

%d bloggers like this: