Restlet edition for Java EE FirstStepsServlet

2024-03-31 14:38

本文主要是介绍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 {@Getpublic 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.*/@Overridepublic 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 routerouter.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 EE FirstStepsServlet的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/864579

相关文章

Java中对象的创建和销毁过程详析

《Java中对象的创建和销毁过程详析》:本文主要介绍Java中对象的创建和销毁过程,对象的创建过程包括类加载检查、内存分配、初始化零值内存、设置对象头和执行init方法,对象的销毁过程由垃圾回收机... 目录前言对象的创建过程1. 类加载检查2China编程. 分配内存3. 初始化零值4. 设置对象头5. 执行

SpringBoot整合easy-es的详细过程

《SpringBoot整合easy-es的详细过程》本文介绍了EasyES,一个基于Elasticsearch的ORM框架,旨在简化开发流程并提高效率,EasyES支持SpringBoot框架,并提供... 目录一、easy-es简介二、实现基于Spring Boot框架的应用程序代码1.添加相关依赖2.添

通俗易懂的Java常见限流算法具体实现

《通俗易懂的Java常见限流算法具体实现》:本文主要介绍Java常见限流算法具体实现的相关资料,包括漏桶算法、令牌桶算法、Nginx限流和Redis+Lua限流的实现原理和具体步骤,并比较了它们的... 目录一、漏桶算法1.漏桶算法的思想和原理2.具体实现二、令牌桶算法1.令牌桶算法流程:2.具体实现2.1

SpringBoot中整合RabbitMQ(测试+部署上线最新完整)的过程

《SpringBoot中整合RabbitMQ(测试+部署上线最新完整)的过程》本文详细介绍了如何在虚拟机和宝塔面板中安装RabbitMQ,并使用Java代码实现消息的发送和接收,通过异步通讯,可以优化... 目录一、RabbitMQ安装二、启动RabbitMQ三、javascript编写Java代码1、引入

spring-boot-starter-thymeleaf加载外部html文件方式

《spring-boot-starter-thymeleaf加载外部html文件方式》本文介绍了在SpringMVC中使用Thymeleaf模板引擎加载外部HTML文件的方法,以及在SpringBoo... 目录1.Thymeleaf介绍2.springboot使用thymeleaf2.1.引入spring

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

Java判断多个时间段是否重合的方法小结

《Java判断多个时间段是否重合的方法小结》这篇文章主要为大家详细介绍了Java中判断多个时间段是否重合的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录判断多个时间段是否有间隔判断时间段集合是否与某时间段重合判断多个时间段是否有间隔实体类内容public class D

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在