restlet-jse

一、实体

Contact 

 

 

package com.tht.common;

import java.io.Serializable;

public class Contact implements Serializable {
	
	private int id;

	private static final long serialVersionUID = 1L;

	private int age;

	private String firstName;

	private Address homeAddress;

	private String lastName;

	public Contact() {
	}

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

	public int getAge() {
		return age;
	}

	public String getFirstName() {
		return firstName;
	}

	public Address getHomeAddress() {
		return homeAddress;
	}

	public String getLastName() {
		return lastName;
	}

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

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

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

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

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Override
	public boolean equals(Object obj) {
	
		return super.equals(obj);
	}
	
	

}

 

 

二、实体  Address

 

package com.tht.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) {
        super();
        this.line1 = line1;
        this.line2 = line2;
        this.zipCode = zipCode;
        this.city = city;
        this.country = country;
    }

    public String getCity() {
        return city;
    }

    public String getCountry() {
        return country;
    }

    public String getLine1() {
        return line1;
    }

    public String getLine2() {
        return line2;
    }

    public String getZipCode() {
        return zipCode;
    }

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

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

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

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

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

}
 

三、服务资源

 

package firstSteps;

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

import com.tht.resource.ContactServerResource;

public class FirstStepsApplication extends Application {

	
	 /**
     * Creates a root Restlet that will receive all incoming calls.
     */
    @Override
    public synchronized Restlet createInboundRoot() {
        // Create a router Restlet that routes each call to a new instance of HelloWorldResource.
        Router router = new Router(getContext());

        // Defines only one route
        router.attach("/hello", ContactServerResource.class);

        return router;
    }
    
   /* public static void main(String[] args) throws Exception {  
        // Create a new Component.  
        Component component = new Component();  
      
        // Add a new HTTP server listening on port 8182.  
        component.getServers().add(Protocol.HTTP, 8182);  
      
        // Attach the sample application.  
        component.getDefaultHost().attach("/firstSteps",  
                new FirstStepsApplication());  
      
        // Start the component.  
        component.start();  
    }          */
}
 

 

package com.tht.resource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.restlet.resource.ServerResource;

import com.tht.common.Address;
import com.tht.common.Contact;
import com.tht.common.ContactResource;

public class ContactServerResource extends ServerResource implements
		ContactResource {
	
	
	public ContactServerResource(){
		
		 
		
	}
	 private static    Contact contact = 
	        new Contact("firstName1", "lastName1", new Address("Address1", "Address2", "20010", "city1", 
            "country1"), 10);

	
	 
	@Override
	public void remove(String key) {
		contact=null;

	}

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

	@Override
	public void store(Contact contact) {
		this.contact=contact;

	}
	
	

}
 

 

 

三启动服务

 

import org.restlet.Component;
import org.restlet.data.Protocol;

import firstSteps.FirstStepsApplication;

/**
 * 启动程序 开起服务http://localhost:8182/firstSteps
 * 
 *可以访问请求路径: http://localhost:8182/firstSteps/hello
 * @author think
 *
 */
public class Run {
	
	 public static void main(String[] args) throws Exception {  
	        // Create a new Component.  
	        Component component = new Component();  
	      
	        // Add a new HTTP server listening on port 8182.  
	        component.getServers().add(Protocol.HTTP, 8182);  
	      
	        // Attach the sample application.  
	        component.getDefaultHost().attach("/firstSteps",  
	                new FirstStepsApplication());  
	      
	        // Start the component.  
	        component.start();  
	    }          

}
 

 

四、启动客户端

package com.tht.client;

import com.tht.common.Contact;
import com.tht.common.ContactResource;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.restlet.data.MediaType;

import org.restlet.resource.ClientResource;
import org.restlet.resource.ResourceException;

public class GetRequest {

	public static void main(String[] args) throws ResourceException,
			IOException {
		ClientResource cr = new ClientResource(
				"http://localhost:8182/firstSteps/hello");

		// Get the Contact object
		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());
			}

		/*
		 * Map<String, Contact> contacts = resource.retrieve(); Contact
		 * contact=null; for(Object key : contacts.keySet()){
		 * System.out.println("key:"+key);
		 * 
		 * contact=contacts.get(key); System.out.println("value:"+contact); if
		 * (contact != null) { System.out.println("firstname: " +
		 * contact.getFirstName()); System.out.println(" lastname: " +
		 * contact.getLastName()); System.out.println("      age: " +
		 * contact.getAge()); }
		 * 
		 * }
		 */

		// In case the Contact object is not available
		// Get a JSON representation of the contact
		// System.out.println("\nJSON representation");
		// cr.get(MediaType.APPLICATION_JSON).write(System.out);
	}
}
 

delete

 

package com.tht.client;
import com.tht.common.Address;
import com.tht.common.Contact;
import com.tht.common.ContactResource;
import java.io.IOException;

import org.restlet.data.MediaType;

import org.restlet.resource.ClientResource;
import org.restlet.resource.ResourceException;

public class DeleteRequest {

    public static void main(String[] args) throws ResourceException,
            IOException {
        ClientResource cr = new ClientResource(
                "http://localhost:8182/firstSteps/hello");
        
        // Get the Contact object
        ContactResource resource = cr.wrap(ContactResource.class);
        Contact contact = 
	        new Contact("firstName1", "lastName1", new Address("Address1", "Address2", "20010", "city1", 
            "country1"), 10);
        
        resource.store(contact);
    
    }
}
 

 

put

 

package com.tht.client;
import com.tht.common.Address;
import com.tht.common.Contact;
import com.tht.common.ContactResource;
import java.io.IOException;

import org.restlet.data.MediaType;

import org.restlet.resource.ClientResource;
import org.restlet.resource.ResourceException;

public class PutRequest {

    public static void main(String[] args) throws ResourceException,
            IOException {
        ClientResource cr = new ClientResource(
                "http://localhost:8182/firstSteps/hello");
        
        // Get the Contact object
        ContactResource resource = cr.wrap(ContactResource.class);
        Contact contact = 
	        new Contact("firstName1", "lastName1", new Address("Address1", "Address2", "20010", "city1", 
	                    "country1"), 10);
        
        resource.store(contact);
    
    }
}
 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值