requestproxy 教程

1.requestproxy 介绍
    requestproxy是基于httpclient做的个工具包,利用了 httpclient 的连接池功能,同时基于几个注解,可以让大家把外部的http接口,轻松的融合到我们项目中,而不是每一个请求都写一个URL,特别适合调用外部API接口较多的项目

2.功能点
  1. 可以使http请求进行接口化,使得http接口更好的和java做融合
  2. 融合json可以自动转为bean
  3. 也可以解析符合要求的xml,基本也是无脑
  4. 负责的返回string,大家可以自己解析
  5. 提供几个通用的签名工具
  6. 支持get和post请求
  7. 提供转码工程

3.spring 配置
    
    <!-- httpclient线程池 -->
     < bean  id = "connectionManagerParams"
         class = "org.apache.commons.httpclient.params.HttpConnectionManagerParams" >
         < property  name = "connectionTimeout"  value = "120000"  />
         < property  name = "soTimeout"  value = "120000"  />
         < property  name = "maxTotalConnections"  value = "30"  />
         < property  name = "defaultMaxConnectionsPerHost"  value = "20"  />
     </ bean >
 
     < bean  id = "connectionManager"
         class = "org.apache.commons.httpclient.MultiThreadedHttpConnectionManager" >
         < property  name = "params"  ref = "connectionManagerParams"  />
     </ bean >
 
     < bean  id = "httpClient"  class = "org.apache.commons.httpclient.HttpClient" >
         < constructor-arg >
             < ref  bean = "connectionManager"  />
         </ constructor-arg >
     </ bean >
 
 
     <!-- 注册 HTTP请求动态代理接口,只有这个是独有的,上面都是通用的 -->
     < bean  class = "com.onepiece.requestproxy.factory.RequestBeanScannerConfigurer" >
         < property  name = "basePackage"  value = "com.jueyue.onepiece.test.request" >
         </ property >
     </ bean >

4.注解使用介绍
    项目是基于注解和接口来实现的,所以注解运用是否熟练,关系到功能是否正常
  •     @IRequest 类注解和spring的@Server等功能相似
  •     @IRequestMethod 方法注解,可不填,默认post请求,60秒超时
  •     @IRequestParam 参数注解  必填,设置参数名称,参数序列,是否签名

参数特殊名称,URL 或者url 表示请求地址,也可以写到@IRequestMethod 

5.实例
    请求接口
    
@IRequest ( "testRequest" )
public  interface  ITestRequest {
 
     @IRequestMethod (type = RequestTypeEnum.GET, url =  "http://api.map.baidu.com/telematics/v3/weather" )
     String testGet( @IRequestParam ( "location" ) String location,
                    @IRequestParam ( "output" ) String output,  @IRequestParam ( "ak" ) String ak);
 
     @IRequestMethod (type = RequestTypeEnum.GET, url =  "http://api.map.baidu.com/telematics/v3/weather" )
     BaiduWeatherEntity testGetEntity( @IRequestParam ( "location" ) String location,
                                      @IRequestParam ( "output" ) String output,
                                      @IRequestParam ( "ak" ) String ak);
 
}


请求百度的天气情况,一个是直接返回字符串,一个是返回对象

     测试用例
public  class  ITestRequestTest  extends  SpringTxTestCase {
 
     @Autowired
     private  ITestRequest testRequest;
 
     @Test
     public  void  testGet() {
         String re = testRequest.testGet( "北京" "json" "5slgyqGDENN7Sy7pw29IUvrZ" );
         System.out.println(re);
 
         BaiduWeatherEntity entity = JSONUtil.parseJson(re, BaiduWeatherEntity. class );
 
         System.out.println(entity.getStatus());
 
         entity = testRequest.testGetEntity( "北京" "json" "5slgyqGDENN7Sy7pw29IUvrZ" );
         System.out.println(JSONUtil.toJson(entity));
 
     }
 
}

    
public  class  BaiduWeatherEntity {
 
     private  String error;
     private  String date;
     private  String status;
}

可以直接注入接口,获得对象

     返回结果

返回的字符串
    
{ "error" : 0 , "status" : "success" , "date" : "2014-12-12" , "results" :[{ "currentCity" : "北京" , "pm25" : "11" , "index" :[{ "title" : "穿衣" , "zs" : "寒冷" , "tipt" : "穿衣指数" , "des" : "天气寒冷,建议着厚羽绒服、毛皮大衣加厚毛衣等隆冬服装。年老体弱者尤其要注意保暖防冻。" },{ "title" : "洗车" , "zs" : "较不宜" , "tipt" : "洗车指数" , "des" : "较不宜洗车,未来一天无雨,风力较大,如果执意擦洗汽车,要做好蒙上污垢的心理准备。" },{ "title" : "旅游" , "zs" : "一般" , "tipt" : "旅游指数" , "des" : "天气较好,温度稍低,而且风稍大,让您感觉有些冷,会对外出有一定影响,外出注意防风保暖。" },{ "title" : "感冒" , "zs" : "易发" , "tipt" : "感冒指数" , "des" : "昼夜温差大,风力较强,易发生感冒,请注意适当增减衣服,加强自我防护避免感冒。" },{ "title" : "运动" , "zs" : "较不宜" , "tipt" : "运动指数" , "des" : "天气较好,但考虑风力较大,天气寒冷,推荐您进行室内运动,若在户外运动须注意保暖。" },{ "title" : "紫外线强度" , "zs" : "弱" , "tipt" : "紫外线强度指数" , "des" : "紫外线强度较弱,建议出门前涂擦SPF在12-15之间、PA+的防晒护肤品。" }], "weather_data" :[{ "date" : "周五 12月12日 (实时:3℃)" , "dayPictureUrl" : "http://api.map.baidu.com/images/weather/day/qing.png" , "nightPictureUrl" : "http://api.map.baidu.com/images/weather/night/qing.png" , "weather" : "晴" , "wind" : "北风4-5级" , "temperature" : "3 ~ -8℃" },{ "date" : "周六" , "dayPictureUrl" : "http://api.map.baidu.com/images/weather/day/qing.png" , "nightPictureUrl" : "http://api.map.baidu.com/images/weather/night/qing.png" , "weather" : "晴" , "wind" : "微风" , "temperature" : "4 ~ -8℃" },{ "date" : "周日" , "dayPictureUrl" : "http://api.map.baidu.com/images/weather/day/qing.png" , "nightPictureUrl" : "http://api.map.baidu.com/images/weather/night/yin.png" , "weather" : "晴转阴" , "wind" : "微风" , "temperature" : "3 ~ -4℃" },{ "date" : "周一" , "dayPictureUrl" : "http://api.map.baidu.com/images/weather/day/yin.png" , "nightPictureUrl" : "http://api.map.baidu.com/images/weather/night/qing.png" , "weather" : "阴转晴" , "wind" : "北风5-6级" , "temperature" : "0 ~ -7℃" }]}]}

对象输出的结构
success
自己返回的对象结构
{ "error" : "0" , "date" : "2014-12-12" , "status" : "success" }

因为是简单的测试,就没有建立复杂的对象

说说优点,可以容融合对象,把没有提供jar的API接口,也想java一样接入进来,json可以自动过滤不想要的数据,建立对象不用那么负责,不用一个一个new URL来处理异常等等

项目地址 点击打开链接
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值