img
The Contact RESTful Web Service
When developing a RESTful-WS application, the first step is to design the service structure, which
includes what HTTP methods will be supported, together with the target URLs for different operations.
For our contact RESTful web services, we want to support query, create, update, and delete
operations. For querying, we want to support retrieving all contacts or a single contact by ID.
The services will be implemented as a Spring MVC controller. The name is the ContactController
class, under the package com.apress.prospring3.ch16.web.restful.controller. The URL pattern, HTTP
method, description, and corresponding controller methods are shown in Table 16-5. For the URLs, they
all use the prefix http://localhost:8080/ch16/restful. In terms of data format, both XML and JSON will
be supported. The corresponding format will be provided according to the accept media type of the
client's HTTP request header.
Table 16-5. Design of RESTful Web Services
URL
HTTP Method Description
Controller Method
To retrieve all contacts
/contact/listdataGET
listData(...)
To retrieve a single contact with the specified ID
/contact/{id}
GET
findContactById(...)
To create a new contact
/contact
POST
create(...)
To update an existing contact with the specified ID update(...)
/contact/{id}
PUT
To delete a contact with ID
/contact
DELETE
delete(...)
Using Spring MVC to Expose RESTful Web Services
In this section, we will show you how to use Spring MVC to expose the contact services as RESTful web
services as designed in the previous section.
First we will create another domain object, the Contacts class. Listing 16-20 shows the Contacts class.
Listing 16-20. The Contacts Class
package com.apress.prospring3.ch16.domain;
import java.io.Serializable;
import java.util.List;
public class Contacts implements Serializable {
private List<Contact> contacts;
public Contacts() {
}
public Contacts(List<Contact> contacts) {
this.contacts = contacts;
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home