2011年4月20日 星期三

初探RESTful-II

Resources
A RESTful resource is anything that is addressable over the Web. By addressable, we mean resources that can be accessed and transferred between clients and servers. Subsequently, a resource is a logical, temporal mapping to a concept in the problem domain for which we are implementing a solution.

if we request a text file from CNN, our browser receives a text file. If we request a Flash movie from YouTube, our browser receives a Flash movie. The data is streamed in both cases over TCP/IP and the browser knows how to interpret the binary streams because of the HTTP protocol response header Content-Type. Consequently, in a RESTful system, the representation of a resource depends on the caller's desired type(MIME type), which is specified within the communication protocol's request.

Representation
the life of a web service there may be a variety of clients requesting resources. Different clients are able to consume different representations of the same resource. Therefore, a representation can take various forms, such as an image, a text file, or an XML stream or a JSON stream, but has to be available through the same URI.

URI
A Uniform Resource Identifier, or URI, in a RESTful web service is a hyperlink to a resource, and it's the only means for clients and servers to exchange representations.(The set of RESTful constraints don't dictate that URIs must be hyperlinks.)

In a RESTful system, the URI is not meant to change over time, as the architecture's implementation is what manages the services, locates the resources, negotiates the representations, and then sends back responses with the requested resources.
ps.在沒有使用RESTful的情況下,若改變檔名,那麼link也會跟著變動

Example:Java讀JSON

Requirement
JSON的官網中,找出相對應的程式語言的套件(我是用這套org.json)

Implementation
import org.json.*;

public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
GenerateJSON myJsonString = new GenerateJSON();
JSONObject tempObj = null;

try {
JSONObject jsonObj = new JSONObject(myJsonString.generateJSON());
tempObj = new JSONObject(jsonObj.get("UserProfile").toString());
System.out.println("/*------------UserProfile---------------*/");
System.out.print(tempObj.get("userName") + " ");
System.out.print(tempObj.get("password") + " ");
System.out.print(tempObj.get("sex"));
System.out.println();

System.out.println("/*------------MoneyPlan---------------*/");
JSONObject moneyPlan = new JSONObject(jsonObj.get("MoneyPlan")
.toString());
tempObj = new JSONObject(moneyPlan.get("Monday").toString());
System.out.print("[Monday]");
System.out.print(tempObj.get("dinner") + "\t");
System.out.print(tempObj.get("breakfast") + "\t");
System.out.print(tempObj.get("lunch") + "\n");

tempObj = new JSONObject(moneyPlan.get("Tuesday").toString());
System.out.print("[Tuesday]");
System.out.print(tempObj.get("dinner") + "\t");
System.out.print(tempObj.get("breakfast") + "\t");
System.out.print(tempObj.get("lunch") + "\n");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}


import org.json.*;
//{
// "MoneyPlan":{
// "Monday":{
// "dinner":"100",
// "breakfast":"50",
// "lunch":"100"
// },
// "Tuesday":{
// "dinner":"200",
// "breakfast":"55",
// "lunch":"150"
// }
// },
// "UserProfile":{
// "userName":"aaa",
// "password":"bbb",
// "Sex":"Boy"
// }
//}

public class GenerateJSON {

public String generateJSON() {
JSONObject jsonObj = new JSONObject();
JSONObject detailObj = new JSONObject();
JSONObject detailObj2 = new JSONObject();

try {
detailObj.put("userName", "9836002");
detailObj.put("password", "9836002");
detailObj.put("sex", "Boy");
jsonObj.put("UserProfile", detailObj);

detailObj2.put("breakfast", "50");
detailObj2.put("lunch", "100");
detailObj2.put("dinner", "100");

detailObj = new JSONObject();
detailObj.put("Monday", detailObj2);

detailObj2 = new JSONObject();
detailObj2.put("breakfast", "55");
detailObj2.put("lunch", "150");
detailObj2.put("dinner", "200");

detailObj.put("Tuesday", detailObj2);
jsonObj.put("MoneyPlan", detailObj);

} catch (JSONException ex) {
System.out.println(ex.toString());
}

return jsonObj.toString();
}
}


Reference:
JSON
JSON in JAVA 的簡單程式範例

2011年4月19日 星期二

初探RESTful-I

最近泰哥要我研究RESTful Service in Codeigniter,雖然tutorial看的懂,能夠寫基本的code,但對tutorial中所提到的REST/RESTful卻還是有點模糊,上網找了些資料來看,找到有一篇還不錯REST and RESTful web service,是石頭大寫的,對RESTful的說明簡單易懂.
後來找到了一本書"RESTful Java Web Services-Master core REST concepts and create RESTful web services in Java",當中也有對REST/REST-like/RESTful做說明.

What is REST?
The term REST comes from Roy Fielding's PhD dissertation, published in 2000, and it stands for REpresentational State Transfer. REST by itself is not an architecture;REST is a set of constraints that, when applied to the design of a system, creates a software architectural style.If we implement all the REST guidelines outlined in Fielding's work, we end up with a system that has specific roles for data, components, hyperlinks, communication protocols, and data consumers.

What is RESTful?
They only define how data is transferred between components and what are the benefits of following the guidelines.
  • It must be a client-server system
  • It has to be stateless—there should be no need for the service to keep users' sessions; in other words, each request should be independent of others
  • It has to support a caching system—the network infrastructure should support cache at different levels
  • It has to be uniformly accessible—each resource must have a unique address and a valid point of access
  • It has to be layered—it must support scalability
  • It should provide code on demand—although this is an optional constraint, applications can be extendable at runtime by allowing the downloading of code on demand, for example, Java Applets
Reference:
REST and RESTful web service
Working with RESTful Services in CodeIgniter