zoukankan      html  css  js  c++  java
  • Annotation 与 HttpClient(5)--Annotation HttpClient

    Annotation HttpClient

    本内容不保证正确性,如有问题请及时提出

    经过前面四篇博客的铺垫,现在给出带有标记的HttpClient的实现。

    1.     带标记的HttpClient的需求和定义

    1)          Http的访问方法

    Http的访问方法主要有两种,分别是GetPost,对应的定义了两个Annotation@Get@Post分别表示实际调用执行的Http消息类型。

    2)          HttpURL

    URL用于定位Http访问的网络服务器地址和方法,对应定义了@URL

    3)          Http参数的类型

    Http访问中,参数可以放在URL中(@ParamUrl)、放在QueryString中(@ParamQuery)、放在Head中(@ParamHead)、放在Body中(@ParamBody)。

    此外对于Multipart类型(文件类型)的消息,定义了@ParamFileHttpFile

    4)          默认的Http参数

    很多情况下,在访问远程的Http请求可以使用默认的参数,针对这种情况,定义了@ParamDefault@ParamDefaults

    5)          返回类型

    我们只实现了两种返回类型,一种是String,一种是byte[]。如果需要其他复杂类型例如xmljson等,可以先返回String,然后再转换成需要的类型。如果需要返回的是文件类型,那么返回类型为byte[].

    2.     项目中的类说明

    MapperProxyMapperMethod,用于创建相应接口的代理,在代理中分析接口的Annotation,组织内部参数,生成调用Http的相应参数。

    RestClientHttpTemplate提供了MapperMethod访问Http服务需要的接口,而DefaultRestClientDefaultHttpTemplate分别是上述两接口的实现。其中DefaultRestClient使用apache HttpClient的连接池形式实现的,而DefaultHttpTemplate则是通过继承SpringHttpTemplate实现的,可以根据需要替换掉这两个实现(比如使用并发的HttpClient)。

    3.     项目使用举例

    3.1 简单的weather访问实例

           Dao定义如下:

    @URL("http://m.weather.com.cn/data/{city}.html")

    publicinterfaceWeatherDao {

     

        @Get

        String getWeather(@ParamUrl("city")int cityCode);

    }

           测试程序如下执行:

           WeatherDao weather = MapperProxy.newMapperProxy(WeatherDao.class,new DefaultHttpTemplate());

           System.out.println(weather.getWeather(101010100));

    3.2 简单的微博访问实例

           Dao定义:

    @URL("https://api.weibo.com/2")

    publicinterface WeiBoHttpDao {

    //获取短url,使用https

        @Get("/short_url/shorten.json")

        public String getshortUrl(@ParamQuery("access_token") String accessToken,

               @ParamQuery("source")long source,

               @ParamQuery("url_long") String urlLong);

    }

    测试程序如下执行:

           WeiBoHttpDao dao = MapperProxy.newMapperProxy(WeiBoHttpDao.class,new DefaultHttpTemplate());

           System.out.println(dao.getshortUrl("token",xxxx,"url"));

    3.3 人人网的访问

    @URL("http://api.m.renren.com/api")

    @ParamDefaults({

        @ParamDefault(paramname="v",value="1.0",type=ParamType.BODY),

        @ParamDefault(paramname="format",type=ParamType.BODY,value="JSON")})

    publicinterface RenRenHttpDao {

     

        @Post("/photos/uploadbin")

        @ParamDefaults({

           @ParamDefault(paramname="voice_rate",value="44100",type=ParamType.BODY),

           @ParamDefault(paramname="default_album_switch",value="2",type=ParamType.BODY),

           @ParamDefault(paramname="from",value="xxxxx",type=ParamType.BODY)

        })

        public String share(//分享

               @ParamBody("access_token") String accessToken,

               @ParamBody("call_id") String callId,

               @ParamBody("caption") String caption,

               @ParamFile("data") HttpFile data,

               @ParamFile("voicedata") HttpFile voiceData,

               @ParamBody("voice_length")long voiceLength,

               @ParamBody("voice_size")long voiceSize,

               @ParamBody("sig") String sig

               );

       

        @Post("/user/getInfo")

        public String userInfo(//获取用户信息

               @ParamBody("access_token") String accessToken,

               @ParamBody("call_id") String callId,

               @ParamBody("type")long type,

               @ParamBody("sig") String sig);

       

        @Post("/friends/getFriends")

        @ParamDefaults({

           @ParamDefault(paramname="hasGender",value="1",type=ParamType.BODY),

           @ParamDefault(paramname="pageSize",value="600",type=ParamType.BODY)

        })

        public String getFriends(//获取好友列表

               @ParamBody("access_token") String accessToken,

               @ParamBody("call_id")long callId,

               @ParamBody("page")long page,

               @ParamBody("sig") String sig

               );

       

    }

    4.     小结

    通过五篇博客,我希望能够将如何编写annotation的应用说明清楚,如果你理解这个过程,会觉得很简单,但是要完全说明白却是一件极其复杂的事情,通过五篇博客的形式,展现了我脑中的annotation应用的实现方式,希望对大家有帮助。

    文中叙述的这种方式,源自于mybatis的源码,大家也可以去参考。

    最后,我将例子中的Annotation HttpClient的源码发出来供大家参考。源码很少,也很简单,并且在我觉得必要的地方加了注释,希望能帮助大家一起探讨学习。

    源码在我的csdn下载里面。http://download.csdn.net/detail/guanxinquan/6019591 

     

     

  • 相关阅读:
    POJ 2240 Arbitrage spfa 判正环
    POJ 3259 Wormholes spfa 判负环
    POJ1680 Currency Exchange SPFA判正环
    HDU5649 DZY Loves Sorting 线段树
    HDU 5648 DZY Loves Math 暴力打表
    HDU5647 DZY Loves Connecting 树形DP
    CDOJ 1071 秋实大哥下棋 线段树
    HDU5046 Airport dancing links 重复覆盖+二分
    HDU 3335 Divisibility dancing links 重复覆盖
    FZU1686 神龙的难题 dancing links 重复覆盖
  • 原文地址:https://www.cnblogs.com/james1207/p/3285860.html
Copyright © 2011-2022 走看看