Upload
kelvin
View
48
Download
0
Embed Size (px)
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
Citation preview
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
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.
Principles of RESTful Programming
• URI-Oriented Designing
• Linking Anything!
• Using the standard Methods of HTTP
• Multi-Representation of Resource
• Stateless Communication
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.
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
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.
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
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.
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{
……
}
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’)
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.
*/
}
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();}}…..
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.*/}
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*/
}
}
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
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.
Special RESTful Web Services
QBE (Query By Example)!
• Asynchronous Message Queue
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’
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’
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--
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
…….
Q&A