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

Restlet edition for Java EE FirstStepsServlet

阅读更多

使有方法

 

一、该项目部署到Tomcat服务器下,启动Tomcat服务器

二、在浏览器地址栏中输入:http://localhost:8080/firstStepsServlet/restlet/hello

输出:成功

 

 

 

方法二:

 

一、不要开启服务器,直接启动类Run

二、在浏览器地址栏中输入:http://localhost:8182/firstSteps/hello

 

 

 

 

OK============================================================

 

更多信息:

 

 


http://wiki.restlet.org/docs_2.0/13-restlet/275-restlet/312-restlet.html

 

 

 

 

Restlet edition for Java EE

Introduction

This chapter presents the Restlet Framework edition for Java EE (Java Enterprise Edition).

This edition is aimed for development and deployment of Restlet applications inside Java EE application server, or more precisely inside Servlet containers such as Apache Tomcat .

Getting started

The rest of this page should get you started with the Restlet Framework, Java EE 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 in a Servlet container
  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 in a Servlet container

Let's now deploy this Restlet application inside your favorite Servlet container. Create a new Servlet Web application as usual, add a "firstStepsServlet" package and put the resource and application classes in. Add the archives listed below into the directory of librairies (/WEB-INF/lib):

  • org.restlet.jar
  • org.restlet.ext.servlet.jar

Then, update the "web.xml" configuration file as follow:

<?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>  
  
   <!-- Restlet adapter -->  
   <servlet>  
      <servlet-name>RestletServlet</servlet-name>  
      <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
      <init-param>
            <!-- Application class name -->
            <param-name>org.restlet.application</param-name>
            <param-value>firstSteps.FirstStepsApplication</param-value>
      </init-param>
   </servlet>  
  
   <!-- Catch all requests -->  
   <servlet-mapping>  
      <servlet-name>RestletServlet</servlet-name>  
      <url-pattern>/*</url-pattern>  
   </servlet-mapping>  
</web-app>  

Finally, package the whole as a WAR file called for example "firstStepsServlet.war" and deploy it inside your Servlet container. Once you have launched the Servlet container, open your favorite web browser, and enter the following URL:

http://<your server name>:<its port number>/firstStepsServlet/hello

The server will happily welcome you with the expected "hello, world" message. You can find the WAR file (packaged with archives taken from Restlet Framework 2.0 Milestone 5) in the "First steps application" files .

Run as a standalone Java application

A Restlet application cannot only run inside a Servlet container, but can also be run as a standalone Java application using a single "org.restlet.jar" JAR.

Create also a main class, copy/paste the following code wich 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 "First steps application" files .

分享到:
评论

相关推荐

    Restlet edition for Java SE firstSteps

    《Restlet Edition for Java SE初探:开启RESTful之旅》 在Java开发领域,当我们谈论RESTful服务时,经常会提到一个强大的工具——Restlet。它是一个完全基于Java的开源框架,专为构建REST(Representational State...

    restlet-jee-2.2.2

    Java EE(Enterprise Edition)是Java平台用于构建企业级应用的规范集,它包含了多种技术,如Servlet、JSP、EJB等。 "restlet-jee-2.2.2"是Restlet框架的一个特定版本,适用于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...

    restlet2.0版本jee源代码

    在Restlet 2.0版本中,它支持Java Enterprise Edition (Java EE) 平台,为开发者提供了在企业环境中构建高效、可扩展的REST服务的能力。 Restlet 2.0版的核心概念包括以下几个方面: 1. **组件(Component)**: ...

    RESTLET开发

    本节将介绍如何使用JEE(Java Enterprise Edition)版本的RESTLET进行开发。 1. **下载RESTLET JEE版本** 首先,需要从官方网站下载RESTLET的JEE版本。假设我们选择的是`restlet-jee-2.0.6.zip`。下载地址:...

    restlet-jee-2.0.6.zip_Restlet 2..0_Restlet framework2.0_org.rest

    标题"restlet-jee-2.0.6.zip_Restlet 2..0_Restlet framework2.0_org.rest"表明这是一个针对Java企业版(Java EE)的Restlet框架2.0.6版本的压缩包,其中包含了与org.restlet相关的组件。 描述中的"restlet框架所需...

    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所需jar包一

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

    Restlet开发的Basic认证

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

    restlet-jee-2.0.3

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

    Java Restlet获取请求头实例

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

    restlet实现最简单的restful webservice

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

    restlet-jse-2.2.1.zip

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

    restlet

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

    RESTLET框架学习书籍

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

    restlet-j2ee-2.0.15.rar

    RESTlet框架提供了简化开发RESTful服务的工具,它在Java EE环境中运行,与传统的基于Servlet的API相比,提供了更为简洁和直观的API。 标题中的"restlet-j2ee-2.0.15.rar"指的是Restlet框架的一个特定版本,即2.0.15...

Global site tag (gtag.js) - Google Analytics