Using Spring 3 to implement RESTful webservice


In this post, I will illustrate how to use Spring framework v.3 to implement RESTful webservices for create, read, update and delete (CRUD) operations.

Relationship between CRUD and HTTP Verbs

The mapping between CRUD and HTTP methods is summarized as below:

  • Create – PUT
  • Read – GET
  • Update – POST
  • Delete – DELETE

Note that browser form element only supports GET and POST. To handle PUT and DELETE, the form will need to send a hidden parameter with name “_method” to the server. For example, to submit a PUT request require following html:

<form action=”put.action” method=”post”>

<input type=”hidden” name=”_method” value=”put”/>

// …

<input type=”submit” value=”Add”/>


Or you can use the form tag in Spring.

Also, the HiddenHttpMethodFilter filter needs to be setup in the web.xml file:


<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>

Note that you will need to get ordering of filters right if you are handling multi-part requests. See the API documentation for more details.

CRUD Operations

As an example, I am going to show the request mappings for implementing the REST web services for CRUD operations on a tag resource in a Spring 3 Controller class.

Create maps to a PUT request in the format http://<mydomain>/tag/add.action

@RequestMapping(value=”/tag/add.action”, method=RequestMethod.PUT)

public String put(@Valid @ModelAttribute(“tag”) TagDTO tagDTO, BindingResult result, Model model) {
// …

Read maps to a GET request in the format http://<mydomain>/tag/1/view.action

@RequestMapping(value=”/tag/{id}/view.action”, method=RequestMethod.GET)
public String view(@PathVariable(“id”) Long id, Model model) {

// …


Update maps to a post request in the format http://<mydomain>/tag/update.action:

@RequestMapping(value=”/tag/update.action”, method=RequestMethod.POST)
public String update(@Valid @ModelAttribute(“tag”) TagDTO tagDTO, BindingResult result, Model model) {
// …

Delete maps to a delete request in the format http://<mydomain>/tag/delete.action

@RequestMapping(value=”/tag/delete.action”, method=RequestMethod.DELETE)

public String delete(@RequestParam(“id”) Long id) {

// …


That’s it for now.

About Raymond Lee
Professional Java/EE Developer, software development technology enthusiast.

Comments are closed.