Listing 16-33. Testing RestTemplate for the Delete Operation
// Testing delete contact
restTemplate.delete(URL_DELETE_CONTACT, 1);
System.out.println("Testing delete contact by id :");
contacts = restTemplate.getForObject(
URL_GET_ALL_CONTACTS, Contacts.class);
listContacts(contacts);
As shown in Listing 16-33, the RestTemplate.delete() method is called, which corresponds to the
HTTP DELETE method, passing in the URL and the ID. Then, all contacts are retrieved and displayed
again to verify the deletion. Running the program again produces the following output (other output
was omitted):
Testing delete contact by id :
Contact - Id: 2, First name: Scott, Last name: Tiger, Birthday: 1990-11-02T00:00:00.000+08:00
Contact - Id: 3, First name: John, Last name: Smith, Birthday: 1964-02-28T00:00:00.000+08:00
As you can see, the contact with an ID of 1 was deleted.
Finally, let's try the insert operation. Add the code snippet in Listing 16-34 into the main() method of
the RestfulClientSample class.
Listing 16-34. Testing RestTemplate for Insert Operation
// Testing create contact
System.out.println("Testing create contact :");
Contact contactNew = new Contact();
contactNew.setFirstName("James");
contactNew.setLastName("Gosling");
contactNew.setBirthDate(new DateTime());
contactNew = restTemplate.postForObject(
URL_CREATE_CONTACT, contactNew, Contact.class);
System.out.println("Contact created successfully: " + contactNew);
As shown in Listing 16-34, a new instance of the Contact object was constructed. Then the
RestTemplate.postForObject() method was called, which corresponds to the HTTP POST method,
passing in the URL, the Contact object we want to create, and the class type. Running the program again
(please restart tc Server first to reinitialize the contact data) will produce the following output:
Testing create contact :
Contact created successfully: Contact - Id: 4, First name: James, Last name: Gosling,
Birthday: 2011-12-01T00:00:00.000+08:00
The contact was created on the server and returned to the client.
Securing RESTful-WS with Spring Security
Any remoting service requires security to restrict unauthorized parties from accessing the service and
retrieving business information or acting on it. RESTful-WS is no exception. In this section, we will
demonstrate how to use the Spring Security project (http://static.springsource.org/spring-
security/site/index.html) to secure the RESTful-WS on the server. In the sample in this section, we will
use Spring Security 3.1 (as of this writing, the latest release is 3.1.0.RELEASE), which provides some
useful support for RESTful-WS.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home