Site icon Soul & Shell Blog

Restlet 實作教學 - Representational State Transfer Web Service

Introduction of Restlet

REST(Representational State Transfer)是一種分散式超媒體系統(如WWW)的軟體架構風格,你可以想像它是一個良好設計的Web應用程式規則。
REST 所描述的網路系統包括三個部份:

REST Principles

Data elements 由標準化介面存取

Components 透過介面傳輸 資源表徵 (representations of resources) 來溝通,而不是直接操作資源本身。

Connectors 提供 component 的抽象介面來溝通,隱藏溝通機制的實作細節。

REST符合以下幾個條件:

應用程式的狀態跟功能拆成 resources
每個 Resource 使用 URI 作為獨一無二的定址方式
所有 Resources 共用相同的介面在 Client 跟 Resource 之間轉換狀態,包括:

A. 定義操作 well-defined operations (在HTTP中即 GET/POST/PUT/DELETE)

B. 有限的內容格式 content types

REST包含以下特色:

A. Client/Server 架構
B. Stateless 狀態無關
C. Cacheable 快取能力
D. Layered 分層能力

符合 REST principles 的系統稱做 RESTful。

Lightweight REST framework, Restlet API

官方網站 http://www.restlet.org/

Restlet on Jetty Implement

載入相關Jar檔

以Eclipse開啟Dynamic Web Project新專案

新增EntryApplication.java

package com.toright.rest;

import org.restlet.Application;
import org.restlet.Context;
import org.restlet.Restlet;
import org.restlet.Router;

public class EntryApplication extends Application {

    public EntryApplication(Context parentContext) {
        super(parentContext);
    }

    @Override
    public synchronized Restlet createRoot() {
        Router router = new Router(getContext());

        //設定URI位置
        router.attach("/users/{user}", UserResource.class);

        //設定預設的處理Class
        router.attachDefault(HelloWorldResource.class);
        return router;
    }

}

新增HelloWorldResource.java

package com.toright.rest;

import org.restlet.Context;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.StringRepresentation;
import org.restlet.resource.Variant;

public class HelloWorldResource extends Resource {

    public HelloWorldResource(Context context, Request request, Response response) {
        super(context, request, response);
        getVariants().add(new Variant(MediaType.TEXT_PLAIN));
    }

    @Override
    public Representation getRepresentation(Variant variant) {
        Representation representation = new StringRepresentation("Hello world", MediaType.TEXT_PLAIN);
        return representation;
    }

}

新增UserResource.java

package com.toright.rest;

import org.restlet.Context;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.StringRepresentation;
import org.restlet.resource.Variant;

public class UserResource extends Resource {

    String userName;
    Object user;

    public UserResource(Context context, Request request, Response response) {
        super(context, request, response);
        System.out.println("Method:"+request.getMethod().getName());
        this.userName = (String) request.getAttributes().get("user");
        getVariants().add(new Variant(MediaType.TEXT_PLAIN));
    }

    @Override
    public Representation getRepresentation(Variant variant) {
        Representation result = null;
        if (variant.getMediaType().equals(MediaType.TEXT_PLAIN)) {
            result = new StringRepresentation("Account of user \"" + this.userName + "\"");
        }
        return result;
    }

}

 編輯 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>first steps servlet</display-name>

    <!-- Application class name -->
    <context-param>
        <param-name>org.restlet.application</param-name>
        <param-value>com.transtep.rest.EntryApplication</param-value>
    </context-param>

    <!-- Restlet adapter -->
    <servlet>
        <servlet-name>RestletServlet</servlet-name>
        <servlet-class>com.noelios.restlet.ext.servlet.ServerServlet</servlet-class>
    </servlet>

    <!-- Catch all requests -->
    <servlet-mapping>
        <servlet-name>RestletServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

 專案的結構如下圖

執行Jetty與測試 http://127.0.0.1:8080/restlet/ 沒有輸入資源位置,自動轉換至預設處理Class,畫面顯示Hello,world

http://127.0.0.1:8080/restlet/users/mary 以資源位置處理相關的功能

Exit mobile version