>  기사  >  데이터 베이스  >  restlet2.1 学习笔记(二) 分别处理Get Post Put请求

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

WBOY
WBOY원래의
2016-06-07 15:31:021508검색

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 "删除电影...";
	}
	
	
}

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

demo02.html



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


	
	
	

访问该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());  
	}
	
	
}



성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.