Supplement: RESTful Web service and JAX-RS Summer 2011 Dr. Chunbo Chu Week 3.

Slides:



Advertisements
Similar presentations
Pierre-Johan CHARTRE Java EE - JAX-RS - Pierre-Johan CHARTRE
Advertisements

Give it a REST already Arnon Rotem-Gal-Oz VP R&D xsights
System Wide Information Management (SWIM)
REST - Representational State Transfer
Representational State Transfer (REST): Representing Information in Web 2.0 Applications this is the presentation Emilio F Zegarra CS 2650.
Service Oriented Architecture
REST Vs. SOAP.
REST Introduction 吴海生 博克软件(杭州)有限公司.
Building and using REST information services Rion Dooley.
Introduction to Web Services
IDU0075 Sissejuhatus veebiteenustesse
REST &.NET James Crisp.NET Practice Lead ThoughtWorks Australia.
Server Access The REST of the Story David Cleary
Creating and Consuming RESTful Web Services with WCF
REST (Representational State Transfer)
Web Service Testing RESTful Web Services Snejina Lazarova Dimo Mitev
What are Web Services? How to use them?
Hypertext Transfer PROTOCOL ----HTTP Sen Wang CSE5232 Network Programming.
Building RESTful Interfaces
Web Services Web Services are the basic fundamental building blocks of invoking features that can be accessed by an application program. The accessibility.
TCP/IP Protocol Suite 1 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 22 World Wide Web and HTTP.
Chapter 3: Programming Web Services Service-Oriented Computing: Semantics, Processes, Agents – Munindar P. Singh and Michael N. Huhns, Wiley, 2005.
World Wide Web1 Applications World Wide Web. 2 Introduction What is hypertext model? Use of hypertext in World Wide Web (WWW) – HTML. WWW client-server.
Controling instrument in the RESTful way
 What is it ? What is it ?  URI,URN,URL URI,URN,URL  HTTP – methods HTTP – methods  HTTP Request Packets HTTP Request Packets  HTTP Request Headers.
CS 415 N-Tier Application Development By Umair Ashraf July 6,2013 National University of Computer and Emerging Sciences Lecture # 9 Introduction to Web.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
CP476 Internet Computing Lecture 5 : HTTP, WWW and URL 1 Lecture 5. WWW, HTTP and URL Objective: to review the concepts of WWW to understand how HTTP works.
Designing and Implementing Web Data Services in Perl
REST.  REST is an acronym standing for Representational State Transfer  A software architecture style for building scalable web services  Typically,
Microsoft Visual Studio 2010 Muhammad Zubair MS (FAST-NU) Experience: 5+ Years Contact:- Cell#:
Microsoft Visual Studio 2010 Muhammad Zubair MS (FAST-NU) Experience: 5+ Years Contact:- Cell#:
Or, Hey can’t we just do it using HTTP for the envelope?
Web Client-Server Server Client Hypertext link TCP port 80.
REST - Introduction Based on material from InfoQ.com (Stefan Tilkov) And slides from MindTouch.com (Steve Bjorg) 1.
SNOWTAM Trial: REST Interface. AIXM XML Developers' Seminar 2 Contents Digital-SNOWTAM Trial Introduction REST Introduction REST in the Digital-SNOWTAM.
1 Seminar on Service Oriented Architecture Principles of REST.
API Crash Course CWU Startup Club. OUTLINE What is an API? Why are API’s useful? What is HTTP? JSON? XML? What is a RESTful API? How do we consume an.
1 WWW. 2 World Wide Web Major application protocol used on the Internet Simple interface Two concepts –Point –Click.
IS-907 Java EE World Wide Web - Overview. World Wide Web - History Tim Berners-Lee, CERN, 1990 Enable researchers to share information: Remote Access.
Representational State Transfer (REST). What is REST? Network Architectural style Overview: –Resources are defined and addressed –Transmits domain-specific.
Service Oriented Programming 1 Javier Espinosa, PhD
RESTful Web Services What is RESTful?
Web Technologies Lecture 10 Web services. From W3C – A software system designed to support interoperable machine-to-machine interaction over a network.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
Representational State Transfer COMP6017 Topics on Web Services Dr Nicholas Gibbins –
Programming for RESTful-SOA An introduction to building a SOA System with light-weighted RESTful Web Services (Web Services without SOAP or WSDL) Xiong.
REST API Design. Application API API = Application Programming Interface APIs expose functionality of an application or service that exists independently.
National College of Science & Information Technology.
RESTful Sevices Distributed Objects Presented by: Shivank Malik
Hypertext Transfer Protocol
API Security Auditing Be Aware,Be Safe
REST- Representational State Transfer Enn Õunapuu
Unit – 5 JAVA Web Services
Advanced Web-based Systems | Misbhauddin
Hypertext Transfer Protocol
Hypertext Transport Protocol
What is REST API ? A REST (Representational State Transfer) Server simply provides access to resources and the REST client accesses and presents the.
Representational State Transfer
Ashish Pandit IT Architect, Middleware & Integration Services
WEB API.
Service-Oriented Computing: Semantics, Processes, Agents
REST på Microsoft-stacken
Service-Oriented Computing: Semantics, Processes, Agents
WCF Data Services and Silverlight
Chengyu Sun California State University, Los Angeles
REST API Design Borrowed heavily from:
Presentation transcript:

Supplement: RESTful Web service and JAX-RS Summer 2011 Dr. Chunbo Chu Week 3

What is a Web service? A software system designed to support interoperable machine-to-machine interaction over a network. It has an interface described in a machine-processable format (e.g.. WSDL)

What is REST? Representational state transfer (REST) is a style of software architecture for distributed hypermedia systems such as the World Wide Web.

What is REST? Application state and functionality are divided into resources Every resource is uniquely addressable using a universal syntax for use in hypermedia links All resources share a uniform interface for the transfer of state between client and resource, consisting of a constrained set of – Well-defined operations – Content types, optionally supporting code on demand

Key REST Principles 1.Give every thing an ID 2.Link things together 3.Use standard methods 4.Resources with multiple representations 5.Communicate statelessly

Give every thing an ID Expose things or collection things with a scheme that is ubiquitous Embrace the URI – How to get it ( or net.tcp:// or net.msmq:// etc.) – Where to get it (example.com) – What to get (customer 1234) An API like this Can be represented like this Customer C = GetCustomer(1234);

Link Things Together Hypermedia as the engine of application state (HATEOAS) – Just means that you should link things together People or apps can transition state by following links Links can be to the same app or to some other app 23

Use Standard Methods public class Resource { Resource(URI u); Response get(); Response post(Request r); Response put(Request r); Response delete(); Response head(); }

Shift to Resource Thinking OperationSQL CommandHTTP Verb Create a resourceINSERTPOST(a), PUT Read a resourceSELECTGET Update a resourceUPDATEPUT, POST(p) Delete a resourceDELETE Query Metadata(Systables)HEAD INSERT INTO CUSTOMERS (ID,...) VALUES (567,...) SQL POST... REST

Shift to Resource Thinking OperationSQL CommandHTTP Verb Create a resourceINSERTPOST Read a resourceSELECTGET Update a resourceUPDATEPUT (POST) Delete a resourceDELETE Query Metadata(Systables)HEAD SELECT FROM CUSTOMERS WHERE ID=567 SQL GET REST

Resources as operations The result of an operation can be considered a resource Operations should be idempotent – Executing them multiple times should have no adverse effect GET should have no lasting effect REST float result = CalculateShipping(Redmond, NYC); API

Content Negotiation Allow the client to ask for what they want I want XML I want JSON I want … (HTML, CSV, etc.) GET /customers/1234 HTTP/1.1 Host: example.com Accept: text/xml GET /customers/1234 HTTP/1.1 Host: example.com Accept: text/json

Communicate Statelessly Stateless means that every HTTP request happens in complete isolation Application State lives on the Client – Everything that is needed to complete the request must be included in the request Resource State lives on the server – Addressable resources are kept in durable storage – May be cached

HTTP GET Remember that GET is supposed to be a safe operation, i.e. the client does not accept any obligations (such as paying you for your services) or assume any responsibility, when all it does is follow a link by issuing a GET. -Stefan Tilkov

Get Orders Returns a collection of orders from the database Issues – Security – you can only see orders you are allowed to see – Paging – stateless requests decide where to start REST API

HTTP POST You can use it to create resources underneath a parent resource and you can use it to append extra data onto the current state of a resource. - RESTful Web Services

HTTP POST POST is ambiguously defined in the HTTP spec POST is the second most used RESTful verb Often referred to as POST(a) for Append – Posting to a collection means to append to that collection – Allows the server to determine the ultimate URI Problem – How to detect duplicate POST requests? Solutions – Use PUT its idempotent by nature – Schemes involving handshaking of some kind between the client and server – Client generated identifier for POST

HTTP PUT PUT is an idempotent way to create / update a resource

HTTP PUT Creates or Updates the resource – Completely replaces whatever was there before with the new content Idempotent by design – Creating or Updating record 123 multiple times should result in the same value – Do NOT do some kind of relative calculation

PUT and IDs If you can allow the client to define an ID within a context that is unique, PUT is easy For example You can map the composite ID (username+id) to a unique ID for the database

RESTFUL SERVICE DESIGN 9 Steps to a RESTFul service

Figure Out the Data Set What data are you exposing to users? You must map the actions to the universal interface AdventureWorks database represents our Data Set.

Split the Data Set into Resources A resource is Anything interesting enough to be the target of a hypertext link Resources are Nouns AdventureWorks has the following Resources Customers – Customers by region Sales – Sales by quarter, region, customer Products – Top selling products

Name The Resources The URI should contain all the scoping information Three basic rules for URI design – Use path to encode hierarchy – Put punctuation characters in path variables to avoid implying hierarchy where there is none – Use query variables to imply inputs to algorithm

Expose a subset of the Uniform Interface Will the client create resources? – Yes – use PUT or POST – Can the client decide the URI? Yes, use PUT No, use POST Will the client update resources? – Yes – support PUT Will the client delete resources? – Yes – support DELETE Will the client fetch resources? – Yes – support GET, HEAD and OPTIONS

Design Your Representations What are you going to return in the body of the request? – Try to use the same representation for send/receive Your representation has to include some state but need not include all – Include a link to other state to get the rest...

The HTTP Response 200 OK 201 Created 330 See Other – map to other URI Headers – Content-Type text/xml text/json text/xhtml+xml Last-Modified – Return the date/time the data was last modified – Client may request If-Modified-Since – If not modified, return 304 Not Modified

What might go wrong? 400 Bad Request 401 Unauthorized 404 Not Found 409 Conflict 425 Unsupported Media Type 500 Internal Server Error 503 Service Unavailable

Summary RESTful services extend the reach of HTTP to your SOA RESTful design is harder than you might think Implementation is easy with WCF

Java Architecture for XML Binding (JAXB)

JAXB Binding Process

Example public class Customer protected int protected String fullname; public Customer() {} public int getId() { return this.id; } public void setId(int id) { this.id = id; } public String getFullName() { return this.fullname; } public void setFullName(String name) { this.fullname = name; } }

Bill Burke

public class Address protected String protected String protected String protected String zip; // getters and setters... }

public class Customer protected int protected String protected Address address; public Customer() {} public int getId() { return this.id; } public void setId(int id) { this.id = id; }... }

Bill Burke 200 Marlboro Street Boston MA 02115

Marshall/unmarshall Customer customer = new Customer(); customer.setId(42); customer.setName("Bill Burke"); JAXBContext ctx = JAXBContext.newInstance(Customer.class); StringWriter writer = new StringWriter(); ctx.createMarshaller().marshal(customer, writer); String custString = writer.toString(); customer = ctx.createUnmarshaller().unmarshal(new StringReader(custString));

Referrences RESTful Java with JAX-RS, RESTful Java with JAX-RS The Java EE 5 JAXB Tutorial