25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 12 ■ Exposing a REST API<br />

Table 12-2 shows the RESTful calls for the <strong>Spring</strong> application. You are going to use and extend the<br />

DocumentService and create the DocumentController class. Your base URI will be<br />

http://localhost:8080/mydocuments/.<br />

Table 12-2. My Documents RESTful Design Over HTTP (http://localhost:8080/ch12/mydocuments/) -<br />

DocumentController<br />

HTTP Method PATH Description DocumentController Method Call<br />

GET /documents Gets all the documents in the repository getDocuments<br />

GET /documents/{id} Finds a document, providing the ID findDocument<br />

POST /documents Creates a new document, will accept a Json<br />

format document<br />

PUT /documents/{id} Updates an existing document, passing an<br />

ID and a Json format document<br />

addDocument<br />

updateDocument<br />

DELETE /documents/{id} Deletes an existing document, passing an ID removeDocument<br />

Let’s review the classes that will be refactored and modified by starting with the DocumentService class in<br />

Listing 12-1.<br />

Listing 12-1. DocumentService.java<br />

package com.apress.isf.java.service;<br />

import java.util.List;<br />

import com.apress.isf.java.model.Document;<br />

public interface DocumentService {<br />

public List getAllDocuments();<br />

public Document findDocumentById(String id);<br />

public Document saveDocument(String id, Document document);<br />

public Document removeDocumentById(String id);<br />

public boolean updateLocationFromDocumentId(String documentId, String location);<br />

}<br />

In Listing 12-1, you added some new methods to the DocumentService class. This class exposes an entire<br />

CRUD (Create-Read-Update-Delete) of the documents. You also added the saveDocument method that you use to<br />

insert or update a document; you added a getAllDocuments method that will get all the documents available, you<br />

added the removeDocumentById that will delete a document. Next, Listing 12-2 shows the implementation of the<br />

DocumentService class.<br />

170

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!