`
thinktothings
  • 浏览: 782476 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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);
    
    }
}
 

 

 

分享到:
评论

相关推荐

    restlet-jse-2.2.1.zip

    在"restlet-jse-2.2.1.zip"这个压缩包中,我们可以期待找到以下内容: 1. **库文件**:包括JAR文件,如`restlet-api.jar`、`restlet-engine.jar`等,这些是Restlet框架的核心组件,提供了处理HTTP请求、响应、资源...

    restlet-jse-2.1.2

    在"restlet-jse-2.1.2"这个压缩包中,我们主要关注的是Restlet框架针对Java标准版(Java SE)的实现。版本号"2.1.2"表明这是一个特定的稳定版本,可能包含了之前版本的改进和修复。 Restlet框架的核心组件包括以下...

    restlet-jse-2.1.1.zip

    Restlet项目为“建立REST概念与Java类之间的映射”提供了一个轻量级而全面的框架。它可用于实现任何种类的REST式系统,而不仅仅是REST式Web服务;而且,事实证明它自从2005年诞生之时起,就是一个可靠的软件。 ...

    restlet.jar

    总的来说,`restlet.jar` 和 `restlet-jse-2.1.4` 是用于构建RESTful服务的重要工具,为Java开发者提供了一套强大且灵活的框架,以遵循REST原则来设计和实现Web服务。通过深入理解和使用Restlet,开发者能够构建高效...

    org.restlet.jar

    org.restlet.jar

    restlet2.0.9包

    2. **JSE模块**:restlet-jse-2.0.9是针对Java SE环境的实现,它提供了一个纯Java的REST客户端和服务器端实现。这个模块包含了一系列类库,使得开发者可以在Java应用中直接使用RESTful API。 3. **API友好**:...

    restlet-junit-tools

    &lt;groupId&gt;org.restlet.jse &lt;artifactId&gt;org.restlet.junit &lt;version&gt;YOUR_VERSION &lt;scope&gt;test ``` 请替换`YOUR_VERSION`为当前的Restlet JUnit工具版本号。 然后,你可以开始编写测试用例。"restlet-junit-...

    restlet笔记

    &lt;groupId&gt;org.restlet.jse &lt;artifactId&gt;org.restlet &lt;version&gt;2.4.0 ``` 接下来是基本的配置步骤,这通常涉及到创建一个`Application`子类并定义路由规则。 ### 创建简单的“Hello World”示例 #### 步骤1:...

    <Reslet1>:Reslet+Spring,配置简单web访问

    &lt;groupId&gt;org.restlet.jse &lt;artifactId&gt;restlet-engine &lt;version&gt;2.4.5 &lt;groupId&gt;org.springframework &lt;artifactId&gt;spring-webmvc &lt;version&gt;5.3.18 ``` 步骤二:配置Spring 创建Spring的配置文件(如:...

Global site tag (gtag.js) - Google Analytics