Chapter 3. Client API

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


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值