现在的位置: 首页 > 综合 > 正文

Chapter 3. Client API

2014年09月05日 ⁄ 综合 ⁄ 共 1659字 ⁄ 字号 评论关闭

3.1. Introduction

3.2. Uniform Interface Constraint

3.3. Ease of use and reusing JAX-RS artifacts

3.4. Getting started with the Jersey client

3.5. Overview of the API

3.6. Adding support for new representations

3.7. Using filters

3.8. Testing services

3.9. Security with Http(s)URLConnection

RESTful Web Service Client Demo
package cn.flyingsoft.rest.client;

import javax.ws.rs.core.MultivaluedMap;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.util.MultivaluedMapImpl;

/**
 * RESTful Web Service客户端
 */
public class RESTClient {
	
	
	public static void main(String[] args){
//		HelloResourceTest();
		HelloResourceTest2();
	}

	private static void HelloResourceTest() {
		//create an instance of a Client
		Client c = Client.create();
		// creates a reference to a Web resource with the URI “http://localhost:8080/RESTWebService/rest”:
		WebResource r = c.resource("http://localhost:8080/RESTWebService/rest");
		
		//Receiving a response
		String responseStr = r.path("hello").get(String.class);
		
		System.out.println(responseStr);
		
	}
	
	private static void HelloResourceTest2(){
		//create an instance of a Client
		Client c = Client.create();
		// creates a reference to a Web resource with the URI “http://localhost:8080/RESTWebService/rest”:
		WebResource r = c.resource("http://localhost:8080/RESTWebService/rest/param");
		
		//
		String userName = "AAA";
		
		//Test @DefaultValue and @PathParam
//		String responseStr = r.path(userName).get(String.class);
		
		//Test One Param : @QueryParam and @PathParam
//		String responseStr = r.path(userName).queryParam("myname", "BBB").get(String.class);

		//Test Multy Params
		MultivaluedMap<String, String> params = new MultivaluedMapImpl();
		params.add("myname", "BBB");
		String responseStr = r.path(userName).queryParams(params).get(String.class);
		
		System.out.println(responseStr);
	}
}

抱歉!评论已关闭.