programming for restful-soa

23
Programming for RESTful- SOA An introduction to building a SOA System with light-weighted RESTful Web Services (Web Services without SOAP or WSDL) Xiong Haoyi [email protected] Huazhong University of Science and Techn ology

Upload: kelvin

Post on 13-Jan-2016

48 views

Category:

Documents


0 download

DESCRIPTION

Programming for RESTful-SOA. An introduction to building a SOA System with light-weighted RESTful Web Services ( Web Services without SOAP or WSDL) Xiong Haoyi xhyccc @ vip.sina.com Huazhong University of Science and Technology. Review of REST. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Programming for RESTful-SOA

Programming for RESTful-SOA

An introduction to building a SOA System

with light-weighted RESTful Web Services

(Web Services without SOAP or WSDL)

Xiong [email protected]

Huazhong University of Science and Technology

Page 2: Programming for RESTful-SOA

Review of REST

• REST, means REpresentational State Transfer, was firstly raised by Roy Thomas Fielding in his doctoral thesis --“Architectural Styles and the Design of Network-based Software Architectures”( University of California - Irvine,2000)

• Rest, broadly used in the web applications, is considered as the basic architecture of web.

• The typical technologies based on REST include WebDAV, Resource-Oriented Web System etc.

Page 3: Programming for RESTful-SOA
Page 4: Programming for RESTful-SOA

Principles of RESTful Programming

• URI-Oriented Designing

• Linking Anything!

• Using the standard Methods of HTTP

• Multi-Representation of Resource

• Stateless Communication

Page 5: Programming for RESTful-SOA

URI-Oriented Designing

• Uniform Resource Identifier--”a compact string of characters used to identify or name a resource on the Internet.”

• URI could be the UUID for any Object in this world.

• The operations over resource could represent as the operations with the URI.

Page 6: Programming for RESTful-SOA

Example of URI and REST

Index to distinct Customer’s Detail with customer id:1234

http://example.com/customerdetails/1234

Index to All Orders in the date of 2007

http://example.com/orders/2007/

Example above Powered by Stefan Tilkov

Page 7: Programming for RESTful-SOA

HTTP Methods and Service Operations

• HTTP Methods including GET, POST, DELETE, PUT and HEAD, in some degree, could map to the CRUD actions which are the typical operations towards to resources.

• Service Operations , binding with semanteme of transcation, focus on the package of complex process and business activity.

Page 8: Programming for RESTful-SOA

REST vs RPC

• REST– Resources—Commands are defined in simple terms:

resources to be retrieved, stored / get, set—difficult to do many joins

• RPC– Calling—Commands are defined in methods with vary

ing complexity: depending on “standard”—easier to hide complex things behind a method

• REST– Nouns—Exchanging resources and concepts

• RPC– Verbs—Exchanging methods

Page 9: Programming for RESTful-SOA

REST in Action

• RestfulBeansServices, as the main result of our research, now is published as an open source software with the apache license on Google Code under the name of “RESTfulCRUDBeans”

• It is a Spring based framework deploying the Pojos as RESTful Web Services.

Page 10: Programming for RESTful-SOA

REST in Action

• Sample Code:• Define a RESTful Web Service with annotation

package org.lightechs.restfulbeans.sample;

import org.lightechs.restfulbeans.annotation.*;

……

@DefaultTransformer(name=“TextureTransformer")/*Index to default Representation Transformer Defined in Spring beans*/

public class SampleRESTfulBean{

……

}

Page 11: Programming for RESTful-SOA

REST in Action• Sample Code:• Define a RESTful Web Service Operation with annotation.....

@ReadMethod /*Reading with GET Method*/

@ResourceURI(URI="hello/${id}")/*URI Designing*/

public String getHelloWorld(@PathParam(name="${id}", converter= "Int“ ) int i)

{

return “Hello!”+i;

}

Invoke this operation with URI:

GET hello/123

Accept: text /* if the accept fragment is invalid, the server would choose default one*/

Result of Server:

Hello!123 (the result depends on the representation of ‘text’)

Page 12: Programming for RESTful-SOA

REST in Action

• Sample Code:Transformer Interface

package org.lightechs.restfulbeans.transformer;

import javax.servlet.http.HttpServletResponse;

public interface transformer{

public void doTransformer(Object obj,HttpServletResponse response);

/*Object means operation result, response means the HTTP response.

Implements this method and define the way reflect in to response stream.

*/

}

Page 13: Programming for RESTful-SOA

REST in Action• Sample Transformer,Transform String into stream

package org.lightechs.restfulbeans.transformer;import javax.servlet.http.HttpServletResponse;import net.sf.json.*;…..

public void doTransformer(Object obj, HttpServletResponse response) {try{ String str=(String)obj;/*if obj is not instance of String, it will throw cast exception to container*/response.setContentType(“plain/text; charset=utf-8");PrintWriter pw=reponse.getWriter();pw.write(str);Pw.close(); }catch(IOException e){ e.printStack();}}…..

Page 14: Programming for RESTful-SOA

REST in Action

• Sample Code: Single String Converter Interface

package org.lightechs.restfulbeans.converter;

public interface SingleStringConverter extends ConvertInterface{

public Object convert(String str);/*str means the part of URI such as “http://....../Operation/${id}”The part named ${id} would convert int to Object as your wish by implementation of this method.*/}

Page 15: Programming for RESTful-SOA

REST in Action

• Sample Converter, Covert String into Integerpackage org.lightechs.restfulbeans.converter;

public class Str2Integer implements SingleStringConverter{

public Object convert(String str) {

return new Integer(str);

/*if this str is not in the format of integer the methods would throws

exception to container*/

}

}

Page 16: Programming for RESTful-SOA

REST in Action

• Sample Multi-String Converter, Covert Strings into File

package org.lightechs.restfulbeans.converter;import java.io.File;public class MultiStr2FilePath implements MultiStringConverter{ public Object convert(String[] strs) {/*Multi-String convert Method*/ String temp=strs[0]; for(int i=1;i<strs.length;i++){ temp+=(File.pathSeparator+strs[i]); } File file=new File(temp); return file; }}Eg: “http://..../operation/Disk:/Path1/Path2/.../File” could index to native file of server side

Page 17: Programming for RESTful-SOA

REST in Action

• Annotation of this container• @PathParam : the Parameter in URI Path.• @NamedParam :named parameter in query String.• @HttpRequest :Mutipart fragment of Http Request (eg. file upload) • @ResourceURI :Mapping the URI with PathParams

(eg.@ResourceURI(“hello/${id}/.../”)-->@PathParam(name=${id},...) )

@DefaultTransformer : firm default transformer for the service such as Json, TableList in sample source.

@ReadMethod,@CreateMethod,@UpdateMethod ,@DeleteMethod could mapping the operation into GET,POST,PUT and DELETE Method.

Page 18: Programming for RESTful-SOA

Special RESTful Web Services

QBE (Query By Example)!

• Asynchronous Message Queue

Page 19: Programming for RESTful-SOA

Query By Example

• URI-Query MappingOriginal URI:

GET db/STUDENT/hust/2005/ceee/

ACCEPT:NAME+CODE+AGE+SEX/COLLEGE+YEAR+DEPARTMEN

T/X-JSON

Mapped SQL:

SELECT NAME, CODE, AGE, SEX

FROM studentWHERE COLLEGE=’hust’ AND YEAR=’2005’ AND DEPARTMENT='ceee’

Page 20: Programming for RESTful-SOA

Query By ExampleDatabase operations RESTful-URI and MIME Type Mapped SQL

update PUT db/STUDENT/hust/001/SamACCEPT COLLEGE+CODE/NAME/X-JSON

UPDATE STUDENT,SET NAME=’Sam’ WHERE COLLEGE=’hust’ AND CODE=’001’

insert POST db/STUDENT/hust/2005/ceee/001/SamACCEPT COLLEGE+YEAR+DEPARTMENT+CODE+NAME+SEX+AGE/X-JSON

INSERT STUDENT(COLLEGE,YEAR,DEPARTMENT,CODE, NAME)VALUE(‘hust’,’2005’,’ceee’,’001’,’Sam’)

delete DELETE db/STUDENT/hust/001ACCEPT COLLEGE+CODE/NAME/X-JSON

DELETE FROM STUDENT,WHERE COLLEGE=’hust’ AND CODE=’001’

Page 21: Programming for RESTful-SOA

Asynchronous Message Queue

• Asynchronous Message Queue over keep-alive HTTP Connection

• The Server ResponseContent-type: multipart/mixed; boundary=”StudentMessage”

--StudentMessage

Content-type: X-JSON

{code:”001”, name:”Sam” ,age=20}

--StudentMessage

Content-type: X-JSON

{code:”002”, name:”Lily” ,age=20}

--StudentMessage--

Page 22: Programming for RESTful-SOA

Introduction to Our Team

• Our team—LighTECHs which is a student team of HUST focus on Open-Source Java Web Container.

• Main Staffs of LighTECHs Xiong Hao-yi : System analyst, Architecture Leader

Bie Rui : Sofware Designer, Chief Programmer

He Lingli : Software Designer, Interrupter

…….

Page 23: Programming for RESTful-SOA

Q&A