zoukankan      html  css  js  c++  java
  • Restlet入门例子 RESTful web framwork for java

    RESTful系列文章索引

    1. Restlet入门例子 - RESTful web framwork for java
    2. [原创]Jersey入门例子

    官方网站介绍:

    http://www.restlet.org/about/introduction

    什么是REST?

    表象化状态转变(英文:Representational State Transfer,简称REST)是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格。

    论文中文下载地址: REST_cn架构风格与基于网络的软件架构设计.pdf

    需要注意的是,REST是设计风格而不是标准。REST通常基于使用HTTPURI,和XML以及HTML这些现有的广泛流行的协议和标准。

    • 资源是由URI来指定。
    • 对资源的操作包括获取、创建、修改和删除资源,这些操作正好对应HTTP协议提供的GET、POST、PUT和DELETE方法。
    • 通过操作资源的表形来操作资源。
    • 资源的表现形式则是XML或者HTML,取决于读者是机器还是人,是消费web服务的客户软件还是web浏览器。当然也可以是任何其他的格式。

    下面是一个简单的restlet的Hello, World的例子

    依赖的jar: org.restlet.jar (使用的版本是restlet-jse-2.0.8)

    定义一个资源

    import org.restlet.Server;
    import org.restlet.data.Protocol;
    import org.restlet.resource.Get;
    import org.restlet.resource.ServerResource;
    
    public class FirstServerResource extends ServerResource {
    	public static void main(String[] args) throws Exception {
    		new Server(Protocol.HTTP, 8182, FirstServerResource.class).start();
    	}
    	
    	@Get
    	public String toString() {
    		return "Hello, World!";
    	}
    	
    }
    

    new Server(Protocol.HTTP, 8182, FirstServerResource.class).start();

    这一句开启了一个web服务器, 这个是restlet自带的服务器(可能是jetty)

    运行结果如下:

    2011-7-1621:15:35 org.restlet.engine.http.connector.HttpServerHelper start
    信息: Starting the internal HTTP server on port
    8182

    获取资源的方式

    import java.io.IOException;
    
    import org.restlet.resource.ClientResource;
    import org.restlet.resource.ResourceException;
    
    public class Client {
    	public static void main(String[] args) throws ResourceException, IOException {
    		new ClientResource("http://localhost:8182/").get().write(System.out);
    	}
    }
    

    运行结果如下:

    2011-7-1621:16:31 org.restlet.engine.http.connector.HttpClientHelper start
    信息: Starting the
    default HTTP client
    Hello, World
    !

    我们可以看到, 在Console中输出了Hello, World!

    可以为资源配置路径

    import org.restlet.Application;
    import org.restlet.Context;
    import org.restlet.Restlet;
    import org.restlet.Server;
    import org.restlet.data.Protocol;
    import org.restlet.routing.Router;
    
    public class ServerApplication extends Application {
    	public static void main(String[] args) throws Exception {
    		new Server(Context.getCurrent(), Protocol.HTTP, 8182, FirstServerResource.class).start();
    	}
    	
    	public ServerApplication(Context context) {
    		super(context);
    	}
    	
    	@Override
    	public Restlet createInboundRoot() {
    		Router router = new Router(this.getContext());
    		router.attach("/hello", FirstServerResource.class);
    		return router;
    	}
    	
    }
    

    这样访问资源的时候就需要访问/hello这个URI

    import java.io.IOException;
    
    import org.restlet.resource.ClientResource;
    import org.restlet.resource.ResourceException;
    
    public class Client {
    	public static void main(String[] args) throws ResourceException, IOException {
    		new ClientResource("http://localhost:8182/hello").get().write(System.out);
    	}
    }
    

    刚开始接触restlet, 还很不熟悉, 了解了一点, 写出来和大家分享一下, 如果有不正确的地方, 请大家指出!

    eclipse工程下载: Restlet.zip

  • 相关阅读:
    298. Binary Tree Longest Consecutive Sequence
    117. Populating Next Right Pointers in Each Node II
    116. Populating Next Right Pointers in Each Node
    163. Missing Ranges
    336. Palindrome Pairs
    727. Minimum Window Subsequence
    211. Add and Search Word
    年底购物狂欢,移动支付安全不容忽视
    成为程序员前需要做的10件事
    全球首推iOS应用防破解技术!
  • 原文地址:https://www.cnblogs.com/icejoywoo/p/2108411.html
Copyright © 2011-2022 走看看