You are on page 1of 20

RESTful Web Services

with JSON and Restlet


COMP6017: Advanced Topics on Web Services

Stephan Scheuermann, Vicky Gohil, Group 5


7 December 2009
Overview
• REST
– REST vs. SOAP
• JSON
– JSON vs. XML
• Restlet Framework
– Architecture
– Extensions
• Demonstration
• Summary

2
What is REST?
• Stands for Representational State Transfer
• REST is a term defined by Roy Fielding in his PhD
dissertation. (2000)
• It is not a standard.
• It is an architectural style for networked systems

• “REST is intended to evoke an image of how a well-designed Web


application behaves: a network of web pages (a virtual state-machine),
where the user progresses through an application by selecting links
(state transitions), resulting in the next page (representing the next state of the
application) being transferred to the user and rendered for their use.” –
Roy Fielding

3
Principles of REST
• Everything gets an URI
– E.g. http://www.unionfilms.com/movies/
– http:// www.unionfilms.com/movies/ hangover/
• Link resources together
• Intuitively understandable URIs, resources, and actions
• Resource based, rather than service based
• Communicate statelessly
• The web itself is built on similar principles

4
Standard HTTP Methods

5
REST vs. SOAP
REST SOAP
• Resources oriented • Services oriented

• Best for Computer/Human • Best for Computer/Computer


Interaction Interaction

• URI: Consistent naming • Lack of standard naming


mechanism for resources mechanism

• Server is stateless • Server may maintain state

• REST security is all about • SOAP security extensions


defined by WS-Security (from
HTTPS
2004)

6
JSON
• JavaScript Object Notation (JSON)
• Subset of the JavaScript Programming Language
• Lightweight data-interchange format
• JSON is text based
• Completely language independent
• Easy for humans to read and write
• Easy for machines to parse and generate

7
Benefits of JSON over XML
• JSON is lighter than XML.

• JSON: Is the native data format for JavaScript and therefore


faster for the browser to read and understand.
• XML: xPath has be used to parse XML

• JSON contains no tags but data and therefore less data to


be transferred between client and the server
• XML uses tags to describe data and tags increase the file
size

8
JSON vs. XML
JSON example XML example
{ “movies” : <?xml version="1.0" ?>

[ <movies>
<movie>
{“year":2009,”name":“Hangover"},
<year>2009</year>
{“year":2009,"name":”2012"}
<name>Hangover</name>
] </movie>
} <movie>
<year>2009</year>
<name>2012</name>
</movie>
</movies>

9
Restlet Framework
• First development framework for REST
– Started in 2005 by Jérôme Louvel
– Enterprise Support provided by Noelios Technologies
– Large community
• Multiple editions for different targets available
– Java SE / EE, Google Web Toolkit, Google App Engine

10
Restlet Framework
• Architecture
– Used as a client and a server framework
– Restlet API defines the concepts
– Restlet Engine implements the API
• Restlet JAR-file only includes basic libraries
– Large number of Extensions is provided

11
Restlet Framework
• Application Object manages a set of related ServerResources
• ServerResource
– Each Object has an URI assigned
• http://localhost:8080/items
– Provides functionality by REST methods
• Get, Put, Post, Delete
• ClientResource
– Communicates with
ServerResource

12
Restlet Framework
• ServerResource implementation leveraging annotations
import org.restlet.resource.ServerResource

public class TestResource extends ServerResource {

@Get(“json”)
public List<TestDataType> retrieve(){
//return data here
}

@Post()
public Representation create(TestDataType data){
//Create a new entry
}

13
Restlet Framework
• Request routing
– Defined in the corresponding Application Object
– Allows to route a specific URI to a ServerResource

import org.restlet.routing.Router

// Create a router
Router router = new Router(getContext());

// Defines a route for our TestResource class


router.attach("/items", TestResource.class);

// Defines a route for another Resource router.attach("/items/


– For example: http://localhost:8080/items/item1
{itemName}", TestResource2.class);

14
Restlet Extensions
• All extensions in the Classpath will be integrated automatically
• ServerConnector
– Restlet JAR-file includes only a lightweight http server
– Servlet extension enables war deployment

• ClientConnector
– Restlet JAR-file allows to connect via http only
– Extensions for https, smtp

15
Restlet Extensions
• Converters
– Serialization of Java Objects is done automatically
– ConverterService for designated type (JSON) is required
– XStream allows to serialize as XML or JSON
@Get(“json”)
public List<TestDataType> retrieve(){
//return data here
}

– XStream processes annotations to configure serialization


@XStreamAlias(value = “film”)
public class Movie implements Serializable

16
Demonstration

17
Summary
• RESTful Web Services looks to be a solid growth area
• Restlet is very easy to use
– Many extensions available
– Very large user community and Enterprise support
– Framework leverages annotations
• Some of the features are new to Restlet 2.0
– This version is still in beta stage

18
Resources
• JSON
– http://www.json.org/
– http://www.json.org/java/
• Restlet Website
– http://www.restlet.org/
• Noelios Technologies offers Enterprise Support
– http://www.noelios.com/

19
Many thanks!
Any questions?

You might also like