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

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

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

restlet处理各种请求方式参考示例_第1张图片

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占位符的值
	@Override
	protected void doInit() throws ResourceException{
		id = Integer.valueOf((String)getRequestAttributes().get("id"));
	}
	
	//处理get请求
	@Override
    public 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 {
	@Override
	public 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请求方式 */
	@Test
	public void testGet() throws IOException {
		String url = "http://localhost:8182/test/hello/1001";
		ClientResource client = new ClientResource(url);
		Representation representation = client.get();// get
		System.out.println("*******get测试结果********");
		System.out.println(representation.getText());
	}

	/** 测试post请求方式 */
	@Test
	public 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); // post
		System.out.println("*******post测试结果********");
		System.out.println(representation.getText());
	}

	/** 测试put请求方式 */
	@Test
	public 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); // put
		System.out.println("*******put测试结果********");
		System.out.println(representation.getText());
	}

	/** 测试delete请求方式 */
	@Test
	public void testDelete() throws IOException {
		String url = "http://localhost:8182/test/hello/1001";
		ClientResource client = new ClientResource(url);
		Representation representation = client.delete();// delete
		System.out.println("*******delete测试结果********");
		System.out.println(representation.getText());
	}

}
7.分别启动服务类Server.java和测试类TestClient.java

 可以看到如下测试结果:

restlet处理各种请求方式参考示例_第2张图片

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

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



你可能感兴趣的:(restlet)