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

相关文章

Maven中引入 springboot 相关依赖的方式(最新推荐)

《Maven中引入springboot相关依赖的方式(最新推荐)》:本文主要介绍Maven中引入springboot相关依赖的方式(最新推荐),本文给大家介绍的非常详细,对大家的学习或工作具有... 目录Maven中引入 springboot 相关依赖的方式1. 不使用版本管理(不推荐)2、使用版本管理(推

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

Java中的工具类命名方法

《Java中的工具类命名方法》:本文主要介绍Java中的工具类究竟如何命名,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Java中的工具类究竟如何命名?先来几个例子几种命名方式的比较到底如何命名 ?总结Java中的工具类究竟如何命名?先来几个例子JD

Java Stream流使用案例深入详解

《JavaStream流使用案例深入详解》:本文主要介绍JavaStream流使用案例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录前言1. Lambda1.1 语法1.2 没参数只有一条语句或者多条语句1.3 一个参数只有一条语句或者多

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

SpringBoot整合OpenFeign的完整指南

《SpringBoot整合OpenFeign的完整指南》OpenFeign是由Netflix开发的一个声明式Web服务客户端,它使得编写HTTP客户端变得更加简单,本文为大家介绍了SpringBoot... 目录什么是OpenFeign环境准备创建 Spring Boot 项目添加依赖启用 OpenFeig

Java Spring 中 @PostConstruct 注解使用原理及常见场景

《JavaSpring中@PostConstruct注解使用原理及常见场景》在JavaSpring中,@PostConstruct注解是一个非常实用的功能,它允许开发者在Spring容器完全初... 目录一、@PostConstruct 注解概述二、@PostConstruct 注解的基本使用2.1 基本代