RestEasy Web服务客户端调用

 RestEasy Web服务客户端调用

个人博客 http://blog.csdn.net/nndtdx

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连不上,我狠郁闷。

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

如果你还不知道如何在GAE中搭建单元测试,可以看该文章。http://blog.csdn.net/nndtdx/article/details/6767352

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

 

 

  
  
  
  
  1. import org.apache.http.HttpEntity; 
  2. import org.apache.http.HttpResponse; 
  3. import org.junit.After; 
  4. import org.junit.AfterClass; 
  5. import org.junit.Before; 
  6. import org.junit.BeforeClass; 
  7. import org.junit.Test; 
  8. import org.junit.Assert; 
  9. import org.junit.internal.runners.statements.Fail; 
  10. import org.apache.http.client.HttpClient; 
  11. import org.apache.http.client.methods.HttpGet; 
  12. import org.apache.http.client.methods.HttpPost; 
  13. import org.apache.http.impl.client.DefaultHttpClient; 
  14. import org.apache.http.util.EntityUtils; 
  15. import org.apache.http.message.BasicNameValuePair; 
  16. import org.apache.http.protocol.HTTP; 
  17. import org.apache.http.NameValuePair; 
  18. import org.apache.http.client.entity.UrlEncodedFormEntity; 
  19. import java.util.*; 

 

  
  
  
  
  1. String urlRoot = "http://localhost:8888/gaerest/"
  2.  
  3.  
  4. @Test 
  5. public void listBooksTest() { 
  6.  
  7.     HttpClient httpClient = new DefaultHttpClient(); 
  8.     String urllistallbook = urlRoot + "library/books/"
  9.  
  10.     HttpGet httpGet = new HttpGet(urllistallbook); 
  11.     try { 
  12.         HttpResponse response = new DefaultHttpClient().execute(httpGet); 
  13.         int code = response.getStatusLine().getStatusCode(); 
  14.         //204服务器已经处理成功,但没有返回任何数据 
  15.         if (code == 200 || code==204) { 
  16.             String content = EntityUtils.toString(response.getEntity()); 
  17.             System.out.println("listaoobook输出===>" + content); 
  18.         } else { 
  19.             System.err.println("建立请求失败,返回状态码==》" + code); 
  20.             Assert.fail("listbooks"); 
  21.         } 
  22.  
  23.     } catch (Exception e) { 
  24.         // TODO: handle exception 
  25.         Assert.fail("listbooks失败" + e.getMessage()); 
  26.     } 
  27.  
  28.  
  29. @Test 
  30. public void  addBookTest() { 
  31.     HttpClient httpClient = new DefaultHttpClient(); 
  32.     String urlString=urlRoot+"library/book/mybook/"
  33.     HttpPost httpPost=new HttpPost(urlString); 
  34.     List<NameValuePair> params=new ArrayList<NameValuePair>(); 
  35.     params.add( new BasicNameValuePair("content""this is my book content")  ); 
  36.     try { 
  37.         UrlEncodedFormEntity entity=new UrlEncodedFormEntity(params,HTTP.UTF_8); 
  38.         httpPost.setEntity(entity); 
  39.         HttpResponse response=httpClient.execute(httpPost); 
  40.         int code= response.getStatusLine().getStatusCode(); 
  41.         if(code==200){ 
  42.             String reString=EntityUtils.toString(response.getEntity()); 
  43.             System.out.println(reString); 
  44.             this.listBooksTest(); 
  45.              
  46.         }else
  47.             Assert.fail("addbooktest===》"+code); 
  48.         } 
  49.            
  50.     } catch (Exception e) { 
  51.         Assert.fail("addbookTest"+e.getMessage()+e.getStackTrace()); 
  52.     } 

 

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

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

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

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

你可能感兴趣的:(REST,客户端,resteasy,调用,休闲)