Home Database Mysql Tutorial restlet2.1 学习笔记(二) 分别处理Get Post Put请求

restlet2.1 学习笔记(二) 分别处理Get Post Put请求

Jun 07, 2016 pm 03:31 PM
get pos deal with study notes

servlet只支持GET与POST两种请求。 但是restlet除了支持GET与POST请求外还支持Delete Put OPTIONS 等多种请求 。 第一步,编写资源类 (可以将资源类想象成Struts2的Action ,每个加上注解的方法都是一个ActionMethod) MovieResource.java package com.zf.r

servlet只支持GET与POST两种请求。

但是restlet除了支持GET与POST请求外还支持Delete  Put  OPTIONS 等多种请求 。


第一步,编写资源类

(可以将资源类想象成Struts2的Action ,每个加上注解的方法都是一个ActionMethod)

MovieResource.java

package com.zf.restlet.demo02.server;

import org.restlet.resource.Delete;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.Put;
import org.restlet.resource.ServerResource;

/**
 * 以3中Method为例
 * @author zhoufeng
 *
 */
public class MovieResource extends ServerResource{
	
	
	@Get
	public String play(){
		return "电影正在播放...";
	}
	
	
	@Post
	public String pause(){
		return "电影暂停...";
	}
	
	@Put
	public String upload(){
		return "电影正在上传...";
	}
	
	@Delete
	public String deleteMovie(){
		return "删除电影...";
	}
	
	
}
Copy after login

第二步,使用html客户端访问(html默认只支持get与post访问。所以下面演示着两种)

demo02.html



<meta charset="UTF-8">
<title>demo02</title>


	
	
	
Copy after login

访问该html通过两个按钮可以发送不同的请求,并会有不同的返回值



第三步:使用Restlet编写客户端调用

MovieClient.java

package com.zf.restlet.demo02.client;

import java.io.IOException;

import org.junit.Test;
import org.restlet.representation.Representation;
import org.restlet.resource.ClientResource;

public class MovieClient {

	@Test
	public void test01() throws IOException{
		ClientResource client = new ClientResource("http://localhost:8888/");
		Representation result =  client.get() ;		//调用get方法
		System.out.println(result.getText());  
	}
	
	@Test
	public void test02() throws IOException{
		ClientResource client = new ClientResource("http://localhost:8888/");  
		Representation result =  client.post(null) ;		//调用post方法
		System.out.println(result.getText());  
	}
	
	
	@Test
	public void test03() throws IOException{
		ClientResource client = new ClientResource("http://localhost:8888/");  
		Representation result =  client.put(null) ;		//调用put方法
		System.out.println(result.getText());  
	}
	
	
	@Test
	public void test04() throws IOException{
		ClientResource client = new ClientResource("http://localhost:8888/");  
		Representation result =  client.delete() ;		//调用delete方法
		System.out.println(result.getText());  
	}
	
	
}
Copy after login



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to delete Xiaohongshu notes How to delete Xiaohongshu notes Mar 21, 2024 pm 08:12 PM

How to delete Xiaohongshu notes

The operation process of WIN10 service host occupying too much CPU The operation process of WIN10 service host occupying too much CPU Mar 27, 2024 pm 02:41 PM

The operation process of WIN10 service host occupying too much CPU

What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? Mar 21, 2024 pm 09:30 PM

What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found?

How to add product links in notes in Xiaohongshu Tutorial on adding product links in notes in Xiaohongshu How to add product links in notes in Xiaohongshu Tutorial on adding product links in notes in Xiaohongshu Mar 12, 2024 am 10:40 AM

How to add product links in notes in Xiaohongshu Tutorial on adding product links in notes in Xiaohongshu

Revealing the appeal of C language: Uncovering the potential of programmers Revealing the appeal of C language: Uncovering the potential of programmers Feb 24, 2024 pm 11:21 PM

Revealing the appeal of C language: Uncovering the potential of programmers

Learn how to handle special characters and convert single quotes in PHP Learn how to handle special characters and convert single quotes in PHP Mar 27, 2024 pm 12:39 PM

Learn how to handle special characters and convert single quotes in PHP

Let's learn how to input the root number in Word together Let's learn how to input the root number in Word together Mar 19, 2024 pm 08:52 PM

Let's learn how to input the root number in Word together

How to publish notes tutorial on Xiaohongshu? Can it block people by posting notes? How to publish notes tutorial on Xiaohongshu? Can it block people by posting notes? Mar 25, 2024 pm 03:20 PM

How to publish notes tutorial on Xiaohongshu? Can it block people by posting notes?

See all articles