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; } }
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 + "]"; } }
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); } }