`
thinktothings
  • 浏览: 777672 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Restlet edition for Java SE firstSteps

阅读更多
http://wiki.restlet.org/docs_2.0/13-restlet/275-restlet/311-restlet.html

Restlet edition for Java SE

Introduction

This chapter presents the Restlet Framework edition for Java SE (Java Standard Edition).

This edition is aimed for development and deployment of Restlet applications inside a regular Java virtual machine using the internal HTTP server of the Restlet Engine, or a pluggable one such as Jetty. This page contains a detailed list of available HTTP server connectors.

Getting started

The rest of this page should get you started with the Restlet Framework, Java SE edition, in less than 10 minutes. It explains how to create a resource that says "hello, world" and run it.

  1. What do I need?
  2. The "hello, world" application
  3. Run as a standalone Java application
  4. Conclusion

What do I need?

We assume that you have a development environment set up and operational, and that you already have installed the Java 1.5 (or higher). In case you haven't downloaded the Restlet Framework yet, select one of the available distributions of the Restlet Framework 2.0 .

The "hello, world" application

Let's start with the core of a REST application: the Resource. Here is the code of the single resource defined by the sample application. Copy/paste the code in your "HelloWorldResource" class.

package firstSteps;

import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;

/**
 * Resource which has only one representation.
 */
public class HelloWorldResource extends ServerResource {

    @Get
    public String represent() {
        return "hello, world";
    }

}

Then, create the sample application. Let's call it "FirstStepsApplication" and copy/paste the following code:

package firstSteps;

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

public class FirstStepsApplication extends Application {

    /**
     * Creates a root Restlet that will receive all incoming calls.
     */
    @Override
    public synchronized Restlet createInboundRoot() {
        // Create a router Restlet that routes each call to a new instance of HelloWorldResource.
        Router router = new Router(getContext());

        // Defines only one route
        router.attach("/hello", HelloWorldResource.class);

        return router;
    }

}   

Run as a standalone Java application

A Restlet application can run inside a regular Java virtual machine or Java Runtime Environment (JRE), using a single "org.restlet.jar" JAR in the classpath. For this we only need to create a Restlet component and associate a HTTP server connector.

Create also a main class, copy/paste the following code which aims at defining a new HTTP server listening on port 8182 and delegating all requests to the "FirstStepsApplication".

public static void main(String[] args) throws Exception {  
    // Create a new Component.  
    Component component = new Component();  
  
    // Add a new HTTP server listening on port 8182.  
    component.getServers().add(Protocol.HTTP, 8182);  
  
    // Attach the sample application.  
    component.getDefaultHost().attach("/firstSteps",  
            new FirstStepsApplication());  
  
    // Start the component.  
    component.start();  
}          

Once you have launched the main class, if you can open your favorite web browser, and gently type the following URL: http://localhost:8182/firstSteps/hello, the server will happily welcome you with a nice "hello, world". Otherwise, make sure that the classpath is correct and that no other program is currently using the port 8182.

You can find the sources of this sample application in the " FirstStepsStandalone (application/zip, 1.4 kB, info )" file .

Conclusion

We hope you that enjoyed these first steps and encourage you to check the equivalent page in the Java EE edition for deployments of the same application in Servlet containers. This can also be a convenient way to deploy your Restlet application in an existing Java EE application server available in your organization.

Notes

Comments (2)

分享到:
评论

相关推荐

    Restlet edition for Java EE FirstStepsServlet

    《Java EE FirstStepsServlet 使用Restlet Edition详解》 在Java EE开发中,Restlet是一个流行的框架,用于构建RESTful Web服务。本篇文章将深入探讨如何使用Restlet Edition进行Java EE的初学者入门,特别是通过一...

    restlet-framework-java:领先的Java REST API框架

    它适用于所有主要平台(Java SE / EE,Google App Engine,OSGi,GWT,Android)的版本,并提供许多扩展以满足所有开发人员的需求。 根据Apache Software License 2.0或Eclipse Public License 1.0的条款,可以...

    org.restlet-2.3.0.jar 最新版本

    org.restlet-2.3.0.jar作为其最新版本,对之前版本进行了优化和增强,提升了性能和兼容性,支持Java SE和Java EE环境,同时也适应了各种现代云平台。 在2.3.0版本中,org.restlet库引入了以下关键特性: 1. **模块...

    restlet处理各种请求方式参考示例

    Restlet是一款轻量级的Java库,专门用于构建RESTful Web服务。REST(Representational State Transfer)是一种架构风格,强调简洁、无状态和基于标准的Web服务设计。本示例将详细阐述如何使用Restlet框架处理HTTP...

    restlet-jse-2.2.1.zip

    Restlet JSE(Java SE)版本是Restlet框架针对Java标准版环境的实现,允许开发者在Java桌面应用或服务器端环境中创建RESTful服务。2.2.1是该框架的一个特定版本,可能包含了一些性能优化、新功能以及对之前版本的bug...

    RESTLET开发

    RESTLET提供了多个版本以适应不同平台的需求,例如Java SE、Java EE、Android等。本节将介绍如何使用JEE(Java Enterprise Edition)版本的RESTLET进行开发。 1. **下载RESTLET JEE版本** 首先,需要从官方网站...

    restlet-jee-2.2.2

    Java EE(Enterprise Edition)是Java平台用于构建企业级应用的规范集,它包含了多种技术,如Servlet、JSP、EJB等。 "restlet-jee-2.2.2"是Restlet框架的一个特定版本,适用于Java EE环境。这个版本的发布旨在为...

    org.restlet.jar

    要确保你的Java环境支持Java SE 5.0(或更高)版本。如果你确实需要的话,可以用Retrotranslator(http://retrotranslator. sourceforge.net/)轻易地把Restlet代码反移植(backport)到J2SE 4.0版上去。

    Restlet所需要的所有jar包

    Restlet是一款开源的Java框架,专门用于构建RESTful(Representational State Transfer)Web服务。REST是一种轻量级的架构风格,常用于构建高效、可扩展的网络应用程序。本压缩包包含Restlet框架运行所需的全部jar...

    Restlet与Spring 集成

    Restlet是一个轻量级的Java框架,专门用于构建REST(Representational State Transfer)架构风格的应用程序。它遵循JAX-RS(Java API for RESTful Web Services)规范,提供了丰富的API来处理HTTP请求和响应,简化了...

    Restlet开发的Basic认证

    Restlet是一个轻量级的Java Web服务开发框架,它提供了构建RESTful(Representational State Transfer)应用程序的工具和API。REST是一种架构风格,强调简洁、无状态和可缓存的网络交互,常用于构建高性能、高可用性...

    restlet所需jar包一

    4. **JAX-RS Support**: Restlet也提供了对Java API for RESTful Web Services (JAX-RS)的支持,如果你的项目需要与JAX-RS兼容,这会很有用。 5. **Testing and Debugging Tools**: 为了帮助开发者测试和调试...

    Java Restlet获取请求头实例

    Restlet 实现ServerResource类 列子有: 返回简单JSON类型 获取请求头,返回请求头 接收简单Json类型数据 将复杂对象使用Json格式返回

    restlet-jee-2.0.3

    Restlet JEE 2.0.3是Restlet框架的一个版本,针对Java企业版(Java Enterprise Edition,简称JEE)环境。 在Java开发中,JAR(Java Archive)文件是一种用于存储Java类、资源和其他元数据的文件格式。Restlet JEE ...

    restlet实现最简单的restful webservice

    1. **设置环境**:首先确保安装了Java Development Kit(JDK),因为Restlet是用Java编写的。然后,下载并添加Restlet JAR文件到你的项目类路径中。 2. **创建资源类**:在Java中创建一个新的类,该类继承自Restlet...

    restlet

    在服务器端,RESTlet充当了一个应用服务器,支持多种Java应用服务器,如Jetty、Tomcat等。它提供了一种模块化的方式来组织和处理HTTP请求,允许开发者定义自定义的资源类来响应特定的URI路径。 客户端部分,RESTlet...

    RESTLET框架学习书籍

    - 在本地Java SE环境或Java EE服务器上部署应用。 - 测试和调试已部署的应用。 #### 五、参考资料 - **附录A:Restlet安装**:详细介绍如何安装配置Restlet环境。 - **附录B:测试工具**:推荐并介绍常用的测试...

    restlet.jar

    在`restlet-jse-2.1.4`中,“jse”表示Java Standard Edition,意味着这是针对Java SE环境的版本,不包括对Java EE服务器的特定集成。版本号“2.1.4”表明这是Restlet框架的第2个主要版本中的第1次次要更新和第4次...

    Restlet开发实例

    JAX-RS是Java API for RESTful Web Services的简称,它是Java平台上的标准,用于创建RESTful服务。在这个实例中,你将学习如何使用Restlet框架结合JAX-RS来定义资源、处理HTTP方法(如GET、POST、PUT、DELETE等)...

Global site tag (gtag.js) - Google Analytics