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

article/2025/9/21 9:25:20

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

1.新建web工程,项目结构如下:


1.编写实体类Student.java:

package test;public class Student {private String name;private String sex;private int age;public Student(String name,String sex,int age){this.name = name;this.sex = sex;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}

3.编写资源处理类FirstServerResource.java:

package test;import org.restlet.data.Form;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.ResourceException;
import org.restlet.resource.ServerResource;//定义一个资源
public class FirstServerResource extends ServerResource {private int id;//用来获取传递过来的id占位符的值@Overrideprotected void doInit() throws ResourceException{id = Integer.valueOf((String)getRequestAttributes().get("id"));}//处理get请求@Overridepublic Representation get(){StringRepresentation str = new StringRepresentation("hello world  " + id);return str;}//处理post请求protected Representation post(Representation entity) {  //获取表单值:Form form = new Form(entity);  String name = form.getFirstValue("name");  String sex = form.getFirstValue("sex");  //将客户端提交的表单值返回return new StringRepresentation(id + "所对应的name:" + name + ",sex:" + sex) ;  }//处理put请求protected Representation put(Representation entity) {  //获取表单值:Form form = new Form(entity);  String name = form.getFirstValue("name");  String sex = form.getFirstValue("sex");  //将客户端提交的表单值返回return new StringRepresentation(id + "所对应的name:" + name + ",sex:" + sex) ;  }//处理delete请求public Representation delete(){//模拟删除//...........return new StringRepresentation(id + "所对应的资源已删除");}}
4.编写服务应用类ServerApplication.java:

package test;import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;//为资源配置路径
public class ServerApplication extends Application {@Overridepublic Restlet createInboundRoot() {Router router = new Router(this.getContext());// 绑定资源路径"hello"到对应的处理资源类(FirstServerResource)// 接收传入的参数"id"router.attach("/hello/{id}", FirstServerResource.class);return router;}}
5.编写组件类MyComponent.java:

package test;import org.restlet.Component;public class MyComponent extends Component {/*** 重写createInboundRoot通过attach方法绑定资源类,并且制定了访问路径* */public MyComponent() {getDefaultHost().attach("/test", new ServerApplication());}
}
6.编写服务启动类Server.java:

package test;import org.restlet.Component;
import org.restlet.data.Protocol;public class Server {public static void main(String[] args) throws Exception {Component component = new MyComponent();  component.getServers().add(Protocol.HTTP, 8182);  component.start();  }
}
7.编写客户端测试类TestClient.java:

package test;import java.io.IOException;import org.junit.Test;
import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.representation.Representation;
import org.restlet.resource.ClientResource;public class TestClient {/** 测试get请求方式 */@Testpublic void testGet() throws IOException {String url = "http://localhost:8182/test/hello/1001";ClientResource client = new ClientResource(url);Representation representation = client.get();// getSystem.out.println("*******get测试结果********");System.out.println(representation.getText());}/** 测试post请求方式 */@Testpublic void testPost() throws IOException {Form form = new Form();form.add("name", "zhuxun");form.add("sex", "M");String url = "http://localhost:8182/test/hello/1001";ClientResource client = new ClientResource(url);// 以post方式提交表单Representation representation = client.post(form); // postSystem.out.println("*******post测试结果********");System.out.println(representation.getText());}/** 测试put请求方式 */@Testpublic void testPut() throws IOException {Form form = new Form();form.add("name", "zhuxun");form.add("sex", "M");String url = "http://localhost:8182/test/hello/1001";ClientResource client = new ClientResource(url);// 以post方式提交表单Representation representation = client.put(form,MediaType.APPLICATION_JAVA_OBJECT); // putSystem.out.println("*******put测试结果********");System.out.println(representation.getText());}/** 测试delete请求方式 */@Testpublic void testDelete() throws IOException {String url = "http://localhost:8182/test/hello/1001";ClientResource client = new ClientResource(url);Representation representation = client.delete();// deleteSystem.out.println("*******delete测试结果********");System.out.println(representation.getText());}}
7.分别启动服务类Server.java和测试类TestClient.java

 可以看到如下测试结果:

注:上面的测试用到了Junit测试工具

如果要下载本项目,可以访问:http://download.csdn.net/detail/u012875880/6607299




http://chatgpt.dhexx.cn/article/aX9sTT6Y.shtml

相关文章

使用Restlet Client发送各种Get和Post请求

在开发web应用时,在对Spring中的Controller进行测试时,需要发送各种get以及post请求进行测试,当然可以自己在浏览器里输入url或者对于测试而言使用Spring提供的MockMvc编写代码进行测试,但是当我们想要测试诸如带Form表格提交&…

Restlet官方指南

Restlet官方指南 Table of contents Restlet overviewRetrieving the content of a Web pageListening to Web browsersOverview of a REST architectureComponents, virtual hosts and applicationsServing static filesAccess loggingDisplaying error pagesGuarding access …

谷歌安装Restlet Client插件

目录 一、Restlet Client插件下载链接二、Restlet Client插件安装步骤 一、Restlet Client插件下载链接 链接: https://pan.baidu.com/s/15rWwbQv6KlTS_T6tcpT_YA 提取码:6mxp 二、Restlet Client插件安装步骤 1、下载完Restlet-Client-v2.8.0.1.zip压…

Restlet指南

cleverpig 发表于 2007-11-30 15:15:48 作者:cleverpig 来源:Matrix 评论数:1 点击数:13,237 投票总得分:5 投票总人次:1 关键字:Restlet,REST,指南,入门 摘要: 当复杂核心化模式日趋强大之时,面向对象设计范例已经不总是Web开发中的最佳选择&#xff0c…

Restlet 学习笔记

摘要:网络上对 restlet 的评判褒贬不一,有的说框架封装的很好,很有弹性,有的说 rest 架构风格本身是一种简单的风格,restlet 过设计以使编程过于复杂,其实我倒不觉得 restlet 有什么复杂,相反很…

Restlet实战(一)Restlet入门资料及概念

先贴上几个本人认为比较有价值,值得初学者一看的文章。 http://www.matrix.org.cn/resource/article/2007-11-30/1312be72-9f14-11dc-bd16-451eadcf4db4.html http://blog.sina.com.cn/s/blog_537c5aab010096v8.html~typev5_one&labelrela_nextarticle http://…

Restlet 2.3 指南

2019独角兽企业重金招聘Python工程师标准>>> #Restlet 2.3 指南 #1. Restlet概述 Restlet框架由两个主要部分构成。首先,一部分是"Restlet API",是一个中立的,支持REST概念,并能促进客户端、服务器端应用程序…

minio用法

1 Minio是在Apache License v2.0下发布的对象存储服务器。它与Amazon S3云存储服务兼容。 它最适合存储非结构化数据,如照片,视频,日志文件,备份和容器/ VM映像。对象的大小可以从几KB到最大5TB。 Minio服务器足够轻&#xff0c…

minio使用

一、介绍 开源协议的对象存储服务,轻量,性能强 二、安装 windows版链接: https://pan.baidu.com/s/1vv2p8bZBeZFG9cpIhDLVXQ?pwds5dd 提取码: s5dd 下载后创建minioData文件用于储存文件 创建run.bat脚本,内容如下 # 设置用户名 set MINIO_ROOT_USERadmin # …

CentOS Minimal 和 NetInstall 版本区别

Index of /centos/7.9.2009/isos/x86_64/ 如图: BinDVD版——就是普通安装版,需安装到计算机硬盘才能用,bin一般都比较大,而且包含大量的常用软件,安装时无需再在线下载(大部分情况)。 minim…

简易最小化虚拟机安装配置(CentOS-7-Minimal)

文章目录 镜像选择虚拟机安装(VMware Workstation)虚拟网络配置(NAT模式)虚拟网卡配置 虚拟机配置静态IP配置及测试系统初始化及库安装停止防火墙 & 关闭防火墙自启动关闭 selinux 防火墙更换镜像源并重建镜像源缓存安装 ifco…

pr双击打开图标没反应,下载ZXPSignLib-minimal.dll替换

微智启今天安装了pr cc2018,双击打开图标无反应 于是又试了Premiere cc2019,还是没反应 桌面还多出一些白色文件图标.crash结尾的 解决方案: 下载ZXPSignLib-minimal.dll文件,微智启软件工作室放到pr安装目录的根目录&#xff…

Minimal Square

文章目录 一、A. Minimal Square总结 一、A. Minimal Square 本题链接:A. Minimal Square 题目: A. Minimal Square time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Find the minimu…

出现minimal bash-like...的问题如何解决?

2021.9.4写下此文,以备查阅。 问题如图: 一般出现这个界面即为引导程序出现问题,根据下面两种情况看待: 卸载双系统之一(比如之前是windeepin双系统,现在卸载了deepin系统)重启时出现。安装新…

Centos教程,DVD、Everything、Minimal、NetInstall区别

今天给大家讲述一下在官网下载Linux中Centos7的时候遇到的版本问题。首先给大家简述一下Centos下载流程。 1.百度搜索Centos,点击官网。 2.点击Download,选择Centos7(举例)。 3.然后这里我们选择aliyun下载。 4.选择第一个镜像版本…

【已解决】grub引导项修复:Minimal BASH-like line editing is supported.

目录 1 问题背景2 问题探索3 问题解决4 告别Bug 1 问题背景 环境: Win10Ubuntu20.04 现象:双系统电脑向移动硬盘安装Ubuntu系统后,重启黑屏并显示Minimal BASH-like line editing is supported. For the first word, TAB lists possible comm…

Centos7 Minimal 版本基本配置记录

每次搭测试环境之前都需要先装一台干净的虚拟机,然而 Centos7 Minimal 版本快速装完之后还需要配置:网络、国内源、一些基础工具(net-tools、vim)等才能远程连接和使用。记录一下,方便下次快速配置使用。 目录 1、网…

详解Minimal Web API的使用

一、简介 “Minimal API 是为了创建具有最小依赖关系的 HTTP API”,这是官方的解释。什么意思呢,创建一个 API 并不需要加载许多的依赖。平时在开发 ASP.NET Core Web API 时,通常需要创建 Controller 来定义我们的 API 这种方式&#xff0c…

实例分割------Yolact-minimal结构详解

yolact结构图 网络backbone可以采用resnet101,resnet50甚至vgg16等。然后有3个分支,1个分支输出目标位置,1个分支输出mask系数,1个分类的置信率,所以决定目标的有4(位置)+k(mask系数)+c(分类置信率)个参数。 检测的大致步骤为: 1.从backbone中取出C3,C4,C5; 2.通…

VMware16安装CentOS 7.9操作系统(Minimal版)

记录:299 场景:使用VMware16安装CentOS 7.9操作系统。 基础环境: 虚拟机:VMware16 操作系统:CentOS 7.9 镜像包:CentOS-7-x86_64-DVD-2009.iso 镜像下载地址: 阿里地址:https…