Presentation Restlet

20
RESTful Web Services with JSON and Restlet COMP6017: Advanced Topics on Web Services Stephan Scheuermann, Vicky Gohil, Group 5 26 June 2022

Transcript of Presentation Restlet

Page 1: Presentation Restlet

RESTful Web Services with JSON and RestletCOMP6017: Advanced Topics on Web Services

Stephan Scheuermann, Vicky Gohil, Group 58 April 2023

Page 2: Presentation Restlet

2

Overview• REST

– REST vs. SOAP

• JSON– JSON vs. XML

• Restlet Framework– Architecture– Extensions

• Demonstration

• Summary

Page 3: Presentation Restlet

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

Page 4: Presentation Restlet

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

Page 5: Presentation Restlet

Standard HTTP Methods

5

Page 6: Presentation Restlet

REST vs. SOAP

REST• Resources oriented

• Best for Computer/Human Interaction

• URI: Consistent naming mechanism for resources

• Server is stateless

• REST security is all about HTTPS

SOAP• Services oriented

• Best for Computer/Computer Interaction

• Lack of standard naming mechanism

• Server may maintain state

• SOAP security extensions defined by WS-Security (from 2004)

6

Page 7: Presentation Restlet

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

Page 8: Presentation Restlet

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

Page 9: Presentation Restlet

JSON vs. XML

JSON example{ “movies” :

[

{“year":2009,”name":“Hangover"},

{“year":2009,"name":”2012"}

]

}

XML example<?xml version="1.0" ?>

<movies>

<movie>

<year>2009</year>

<name>Hangover</name>

</movie>

<movie>

<year>2009</year>

<name>2012</name>

</movie>

</movies>

9

Page 10: Presentation Restlet

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

Page 11: Presentation Restlet

Restlet Framework

11

• 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

Page 12: Presentation Restlet

Restlet Framework

12

• 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

Page 13: Presentation Restlet

Restlet Framework

13

• ServerResource implementation leveraging annotationsimport 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}

}

Page 14: Presentation Restlet

Restlet Framework

14

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

– For example: http://localhost:8080/items/item1

import org.restlet.routing.Router

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

// Defines a route for our TestResource classrouter.attach("/items", TestResource.class);

// Defines a route for another Resource router.attach("/items/{itemName}", TestResource2.class);

Page 15: Presentation Restlet

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

Page 16: Presentation Restlet

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

– XStream processes annotations to configure serialization

16

@Get(“json”)public List<TestDataType> retrieve(){

//return data here}

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

Page 17: Presentation Restlet

Demonstration

17

Page 18: Presentation Restlet

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

Page 19: Presentation Restlet

19

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/

Page 20: Presentation Restlet

Many thanks!Any questions?