resteasy 客户端java_RestEasy Web服务客户端调用

RestEasy Web服务客户端调用

l 背景

在前几篇文章中,我介绍了用RestEasy 框架搭建的Rest风格的java web服务。这几天事情比较多,一直没有写客户端的调用方法。现在就谈一下客户端的调用方法。

首先,如果你还不知道如何利用RestEasy 构建Rest风格的Web服务。请查看与web服务相关的概念以及如何利用RestEasy搭建webservice的文章。

l 编码测试

本来想用Android做个客户端,然后将测试代码写在android应用程序中。结果发现我将搭载有webservice的gae运行起来之后,利用http://ip:port不能在Android客户单中访问。令人郁闷。浏览器中输入http://localhost:port 居然没有问题,可以正常调用使用。我想肯定在Android中是无法解析localhost.自然无响应。我试图利用本机ip问。结果以在GAE平台上,run之后,只能用http://localhost:port访问,使用ip连不上,我狠郁闷。

到最后,不得不写了几个测试用例。

下面是代码,这里主要测试两个方法,一个是listallbook即get方式提交,另外一个是addbook使用post提交。方法如下

importorg.apache.http.HttpEntity;

importorg.apache.http.HttpResponse;

importorg.junit.After;

importorg.junit.AfterClass;

importorg.junit.Before;

importorg.junit.BeforeClass;

importorg.junit.Test;

importorg.junit.Assert;

importorg.junit.internal.runners.statements.Fail;

importorg.apache.http.client.HttpClient;

importorg.apache.http.client.methods.HttpGet;

importorg.apache.http.client.methods.HttpPost;

importorg.apache.http.impl.client.DefaultHttpClient;

importorg.apache.http.util.EntityUtils;

importorg.apache.http.message.BasicNameValuePair;

importorg.apache.http.protocol.HTTP;

importorg.apache.http.NameValuePair;

importorg.apache.http.client.entity.UrlEncodedFormEntity;

importjava.util.*;

String urlRoot ="http://localhost:8888/gaerest/";

@Test

publicvoidlistBooksTest() {

HttpClient httpClient = newDefaultHttpClient();

String urllistallbook = urlRoot + "library/books/";

HttpGet httpGet = newHttpGet(urllistallbook);

try{

HttpResponse response = newDefaultHttpClient().execute(httpGet);

intcode = response.getStatusLine().getStatusCode();

//204服务器已经处理成功,但没有返回任何数据

if(code ==200|| code==204) {

String content = EntityUtils.toString(response.getEntity());

System.out.println("listaoobook输出===>"+ content);

} else{

System.err.println("建立请求失败,返回状态码==》"+ code);

Assert.fail("listbooks");

}

} catch(Exception e) {

// TODO: handle exception

Assert.fail("listbooks失败"+ e.getMessage());

}

}

@Test

publicvoidaddBookTest() {

HttpClient httpClient = newDefaultHttpClient();

String urlString=urlRoot+"library/book/mybook/";

HttpPost httpPost=newHttpPost(urlString);

List params=newArrayList();

params.add( newBasicNameValuePair("content","this is my book content")  );

try{

UrlEncodedFormEntity entity=newUrlEncodedFormEntity(params,HTTP.UTF_8);

httpPost.setEntity(entity);

HttpResponse response=httpClient.execute(httpPost);

intcode= response.getStatusLine().getStatusCode();

if(code==200){

String reString=EntityUtils.toString(response.getEntity());

System.out.println(reString);

this.listBooksTest();

}else{

Assert.fail("addbooktest===》"+code);

}

} catch(Exception e) {

Assert.fail("addbookTest"+e.getMessage()+e.getStackTrace());

}

}

这里主要是用了Apache提供的几个类。构建不同的请求方式对象,然后调用HttpClient进行执行。不过需要注意,上边很多类在Android中也有提供相同的类,写的时候,注意选择是那个里边的类。

GET方式很好理解,就是请求一个url即可。Post方式只需要将数据放在请求实体中即可。

运行单元测试,完美通过。查看输出,可以看到列出的数目已经增加了,说明post方法没有问题。

接下来还要处理一个gae的webservice如果在开发阶段,我如何将其发布到本地,使其通过本地ip在模拟器中可以访问呢?思考中。目前就先依赖单元测试吧。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值