25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 12<br />

Exposing a REST API<br />

Nowadays most of the big social network companies like Facebook and Twitter expose some services through<br />

REST API calls, making them easy to integrate with other applications. In this chapter, you are going to extend your<br />

code by adding some new functionality. You are going to expose a RESTful API so other people can easily send,<br />

receive, update, and remove documents from your <strong>Spring</strong> application over the HTTP protocol. And yes, this is a web<br />

application too!<br />

RESTful<br />

Before you start coding, let’s start designing the RESTful calls. I’ll also review what this RESTful thing is. Roy Fielding<br />

introduced the Representational State Transfer (REST) in 2000 as a doctoral dissertation, and it has been adopted<br />

for exposing a new architectural style for the Web with a set of constraints that are applied to components and data<br />

elements, simplifying implementations, reducing the complexity, and helping to improve performance. Table 12-1<br />

shows how these architectural constraints describe a uniform interface for accessing different resources over HTTP.<br />

Table 12-1. Representation for Resources<br />

Representation Description<br />

GET<br />

Gets the resource<br />

POST<br />

Creates a new resource<br />

PUT<br />

Updates a new resource<br />

DELETE<br />

Deletes the resource<br />

Table 12-1 shows the HTTP standard methods that are also applied to Web Service APIs as an alternative to SOAP<br />

(Simple Object Access Protocol). The RESTful APIs have the following features:<br />

• They use an internet media type for the data, like JSON, XML, Images, Atom, etc.<br />

• A base URI such as http://mydocuments.com/resources/<br />

• Hyperlinks to reference state<br />

• Hyperlinks to reference resources<br />

• They use HTTP standard methods: GET, POST, PUT, DELETE, TRACE, OPTIONS,<br />

CONNECT, PATCH.<br />

169

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

Saved successfully!

Ooh no, something went wrong!