fb_license

技術標籤

@selector (1) 初使化區塊 (1) 物件 (1) 物件導向 (2) 型別 (4) 封裝 (1) 流程控制 (1) 陣列 (3) 推論型別 (2) 實機測試 (1) 蓋索林(gasolin) (1) 模組 (1) 憑證 (1) 轉型 (1) 羅康鴻 (121) 類別 (1) 變數 (5) Accelerometer (1) ActiveRecord (1) Activity (1) AFNetworking (1) alloc (1) Android (3) Animation (1) App (1) App ID (1) APP上傳 (1) ASP.NET (1) AVAudioPlayer (1) block (1) C# (2) class (1) CLLocationManager (1) CLLocationManagerDelegate (1) CMMotionManager (4) Controller (1) delegate (1) DELETE語法 (1) Device Motion資料 (1) Dialog (1) DropDownList (1) dynamic language (2) Facebook SDK (9) FBRequest (5) FBRequestConnection (2) FMDB (1) Gesture Recognizers (6) GROUP BY (2) Gyro (1) HAVING (1) IBAction (1) IBOutlet (1) id (3) inheritance (1) init (1) Insert (1) instance variable (1) Interface Builder (1) iOS (70) iOS idea (7) iOS Introduction (1) Layout (1) Magnetometer (1) Menu (2) Method (2) MKMapView (1) MKPointAnnotation (1) MS SQL (5) Nil (1) NSArray (1) NSDictionary (1) NSError (1) NSFileManager & .plist (1) NSMutableArray (1) NSMutableDictionary (1) NSNotificationCenter (1) NULL (1) object (2) Objective-C (16) Objective-C idea (1) ORDER BY (1) Parameter (1) property (1) protocol (2) Provisioning (1) Proximate Sensor (1) Q and A (2) R類別 (1) Rails (9) RESTful SOA (9) Ruby (8) Scene (1) SEELECT (1) Segue (2) SEL (1) SELECT語法 (1) Shake (1) Simulator (1) SOA (8) SQL (6) SQL Server (5) SQL函數 (1) SQL彙總函數 SQL (1) SQLite (1) Storyboard (1) Style (1) Swift (1) Table (1) target & action (1) Theme (1) Toast (1) TRUNCATE TABLE語法 (1) UIActionSheet (1) UIActionSheetDelegate (1) UIActivityIndicatorView (1) UIAlertView (1) UIBarButtonItem (1) UIButton (1) UICollectionView (1) UICollectionViewDataSource (1) UIControl (9) UIDatePicker (1) UIImage (1) UIImagePickerController (2) UIImagePickerControllerDelegate (2) UIImageView (1) UILabel (1) UILongPressGestureRecognizer (1) UINavigationController (2) UIPanGestureRecognizer (1) UIPinchGestureRecognizer (1) UIProgressView (1) UIResponder (1) UIRotationGestureRecognizer (1) UISegmentedControl (1) UISlider (1) UIStepper (1) UISwipeGestureRecognizer (1) UISwitch (1) UITabBarController (1) UITableView (1) UITableViewDataSource (1) UITapGestureRecognizer (1) UITextField (1) UITextFieldDelegate (1) UITextView (2) UITextViewDelegate (1) UIToolBar (1) UIView (8) UIWebView (1) UPDATE語法 (1) var (2) VB.NET (7) View (4) WHERE子句 (1) XML (1)

2013/10/11

[Rails] RESTful SOA Part II using Rails Routes & Controller


原來要完成RESTful SOA,在Rails中只需要設定RoutesController就好!




RESTful SOA是一個使用HTTP並遵循REST原則的SOA。它從以下三個方面資源進行定義:
  • URI,比如:http://example.com/resources/
  • Web服務接受與返回的網際網路媒體類型,比如:JSONXML ,YAML 等。
  • Web服務在該資源上所支持的一系列HTTP Method(比如:POST,GET,PUT或DELETE)。

(資料參考:http://zh.wikipedia.org/wiki/REST)



以CRUD(Create, Read, Update, Delete)來看URI與HTTP Method的對應,
格式為[HTTP Method] URI

Create:[POST] http://host/users  => 建立User資料
Read:[GET] http://host/users  => 讀取所有User資料
Read:[GET] http://host/users/1  => 讀取編號為1的User資料
Update:[PUT] http://host/users/1  => 更新編號為1的User資料
Delete:[DELETE] http://host/users/1  => 刪除編號為1的User資料




現在讓我們透過Routes與Controller來實現RESTfulCRUD中的R Read功能,資料格式方式,則以JSON作為回傳的資料格式:


1. 建立RESTful SOA Service - rails new
  • 開啟終端機
  • 輸入rails new user_serivce_2,建立名為user_service_2的SOA服務





2. routes.rb 設定"Read"URI
  • 於[your service application]\config找到routes.rb
  • 接下來我們將透過"get"指令宣告URI並對應到Controller,讓Controller對資源的請求進行回應,get格式如下:get "[URI]" => "[Controller]#[Method],同時透過:defaults參數來設定資料的格式預設為json。
    • 取所有user,URI為"users",處理URI請求的Cronoller為users,對應方法為read_all
    • 讀取特定編號的user,URL為"users/:id",其中的:id表示透過URI傳入user的編號,處理URI請的Controller為users,對應的方法為read
UserService2::Application.routes.draw do get 'users' => 'users#read_all', :defaults => {:format => "json"} get 'users/:id' => 'users#read', :defaults => {:format => "json"} end

3. Controller處理URI請求 - rails generate controller

  • 開啟終端機,並切換到user_service_2目錄下
  • 敲入"rails generate controller users read_all read"指令並執行,建立名為users的Controller,並在建立的同時建立read_all與read方法

  • 於[your service application]\controllers,開啟users_controller.rb
  • 加入程式,建立將user資料建立成hash,並透過render方法與:json參數將資料轉換為json資料回傳。在此僅建立2筆資料,read_all會1次回傳這2筆資料,而read則會依傳入的編號回傳對應的user資料
class UsersController < ApplicationController def read_all #使用array裝2個Hash,表示2筆資料 #透過render指定json參數, #此時render會獎資料轉為json回傳。 render json: [ { :id => "1", :name => "khlo", :age => "32", :gender => "1" }, { :id => "2", :name => "Nicole", :age => "28", :gender => "0" } ] end def read #依id回傳對應的資料 if(params[:id] == "1") #透過render指定json參數回傳json資料。 render json: { :id => "1", :name => "khlo", :age => "32", :gender => "1" } elsif (params[:id] == "2") #透過render指定json參數回傳json資料。 render json: { :id => "2", :name => "Nicole", :age => "28", :gender => "0" } else #若找無資料,則不回傳資料 head :no_content end end end


4. 啟動伺服器 - rails server
  • 輸入rails server,透過rails server啟動伺服器,其預設的port為3000:


5. 測試 - Postman
  • Read 所有user
    • URI:http://localhost:3000/users
    • HTTP Method:GET
    • Data:(none)

  • Read 單一user
    • URI:http://localhost:3000/users/1
    • HTTP Method:GET
    • Data:(none)



檔案下載:user_service_2.zip


當然,針對CRUD中的C(Create)、U(Update)、D(Delete),也同樣可以在routes.rb中宣告CUD的URI,如下:
UserService2::Application.routes.draw do #C:create post "users" => "users#create", :defaults => {:format => "json"} #U:Update put "users/:id" => "users#update", :defaults => {:format => "json"} #D:Delete post "users/:id" => "users#delete", :defaults => {:format => "json"} get "users" => "users#read_all", :defaults => {:format => "json"} get "users/:id" => "users#read", :defaults => {:format => "json"} end

同樣的,建立對應Controller的方法,如下:

class UsersController < ApplicationController def create end def update end def delete end def read_all #使用array裝2個Hash,表示2筆資料 #透過render指定json參數, #此時render會獎資料轉為json回傳。 render json: [ { :id => "1", :name => "khlo", :age => "32", :gender => "1" }, { :id => "2", :name => "Nicole", :age => "28", :gender => "0" } ] end def read #依id回傳對應的資料 if(params[:id] == "1") #透過render指定json參數回傳json資料。 render json: { :id => "1", :name => "khlo", :age => "32", :gender => "1" } elsif (params[:id] == "2") #透過render指定json參數回傳json資料。 render json: { :id => "2", :name => "Nicole", :age => "28", :gender => "0" } else #若找無資料,則不回傳資料 head :no_content end end end