Restlet 学习笔记(一)

本文介绍了一个基于Restlet框架的应用示例,包括服务器端资源定义、客户端请求处理等关键环节,并展示了如何通过HTTP协议进行交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基于 Restlet Version 2.0.7 (stable)

 

J2SEClient

 

org.wp.example.server

 

FirstApplication

package org.wp.example.server;

import org.restlet.Application;
import org.restlet.Component;
import org.restlet.Restlet;
import org.restlet.data.Protocol;
import org.restlet.routing.Router;

public class FirstApplication extends Application {
	public static void main(String args[]) throws Exception {
		Component component = new Component();
		component.getServers().add(Protocol.HTTP, 8182);
		component.getDefaultHost().attach(new FirstApplication());
		component.start();
	}

	@Override
	public Restlet createInboundRoot() {
		Router router = new Router(getContext());
		router.attach("/contacts/123", ContactServerResource.class);
		return router;
	}
}

 

ContactServerResource

package org.wp.example.server;

import org.restlet.resource.Delete;
import org.restlet.resource.Get;
import org.restlet.resource.Put;
import org.restlet.resource.ServerResource;
import org.wp.example.common.Address;
import org.wp.example.common.Contact;
import org.wp.example.common.ContactResource;

public class ContactServerResource extends ServerResource implements ContactResource {
	private static volatile Contact contact = new Contact("Scott", "Tiger", 40,
			new Address("10 bd Google", null, "20010", "Mountain View", "USA"));

	@Delete
	public void remove() {
		contact = null;
	}

	@Get
	public Contact retrieve() {
		return contact;
	}

	@Put
	public void store(Contact contact) {
		ContactServerResource.contact = contact;
	}
}

 

org.wp.example.common

ContactResource

package org.wp.example.common;

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

public interface ContactResource {

	@Get
	public Contact retrieve();

	@Put
	public void store(Contact contact);

	@Delete
	public void remove();
}

 

Contact

package org.wp.example.common;

import java.io.Serializable;

public class Contact implements Serializable {
	private static final long serialVersionUID = 1L;

	private String firstName;
	private String lastName;
	private int age;
	private Address homeAddress;

	public Contact() {
	}

	public Contact(String firstName, String lastName, int age,
			Address homeAddress) {
		this.firstName = firstName;
		this.lastName = lastName;
		this.age = age;
		this.homeAddress = homeAddress;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Address getHomeAddress() {
		return homeAddress;
	}

	public void setHomeAddress(Address homeAddress) {
		this.homeAddress = homeAddress;
	}

	@Override
	public String toString() {
		return "Contact [age=" + age + ", firstName=" + firstName
				+ ", homeAddress=" + homeAddress + ", lastName=" + lastName
				+ "]";
	}
}

 

Address

package org.wp.example.common;

import java.io.Serializable;

public class Address implements Serializable {
	private static final long serialVersionUID = 1L;

	private String line1;
	private String line2;
	private String zipCode;
	private String city;
	private String country;

	public Address() {
	}

	public Address(String line1, String line2, String zipCode, String city,
			String country) {
		this.line1 = line1;
		this.line2 = line2;
		this.zipCode = zipCode;
		this.city = city;
		this.country = country;
	}

	public String getLine1() {
		return line1;
	}

	public void setLine1(String line1) {
		this.line1 = line1;
	}

	public String getLine2() {
		return line2;
	}

	public void setLine2(String line2) {
		this.line2 = line2;
	}

	public String getZipCode() {
		return zipCode;
	}

	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	@Override
	public String toString() {
		return "Address [city=" + city + ", country=" + country + ", line1="
				+ line1 + ", line2=" + line2 + ", zipCode=" + zipCode + "]";
	}
}

 

 

org.wp.example.client

 

J2SEClient

package org.wp.example.client;

import org.restlet.resource.ClientResource;
import org.wp.example.common.Contact;
import org.wp.example.common.ContactResource;

public class J2SEClient {
	public static void main(String args[]) throws Exception {
		ClientResource cr = new ClientResource("http://localhost:8182/contacts/123");

		// 获得联系人对象
		ContactResource resource = cr.wrap(ContactResource.class);
		Contact contact = resource.retrieve();
		if (contact != null) {
			System.out.println("firstname: " + contact.getFirstName());
			System.out.println(" lastname: " + contact.getLastName());
			System.out.println("      age: " + contact.getAge());
			System.out.println("  address: " + contact.getHomeAddress().toString());
		}

		// System.out.println("\nJSON representation");
		// cr.get(MediaType.APPLICATION_JSON).write(System.out);
	}
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值