[关闭]
@changedi 2015-11-25T17:00:23.000000Z 字数 5385 阅读 2398

http-funny——一个轻量的http调用框架

框架


http-funny是一个快捷的http服务调用框架。主要功能就是帮助java开发者统一配置管理各种http服务。

主要特点:

使用示例:

详见代码 \src\test\java\com\github\changedi\http\core\demoservice

首先配置用到的http服务,见BaiduHttpService.java

  1. @Scheme
  2. @Host("apis.baidu.com")
  3. public abstract class BaiduHttpService {
  4. @HttpMethod
  5. @Path("/apistore/{pathId}/citylist")
  6. public abstract Map<String, Object> listcity(
  7. @QueryParam Map<String, Object> querys,
  8. @PathParam Map<String, Object> paths,
  9. @HeaderParam Map<String, Object> headers);
  10. @HttpMethod(HttpMethodEnum.GET)
  11. @Path("/apistore/weatherservice/citylist")
  12. @Serialization(SerializationEnum.JSON)
  13. public abstract Map<String, Object> listcity2(
  14. @QueryParam Map<String, Object> querys,
  15. @PathParam Map<String, Object> paths,
  16. @HeaderParam Map<String, Object> headers, Callback callback);
  17. @HttpMethod(HttpMethodEnum.POST)
  18. @Path("/apistore/idlocr/ocr")
  19. @Serialization(SerializationEnum.JSON)
  20. public abstract Map<String, Object> ocr(
  21. @QueryParam Map<String, Object> querys,
  22. @PathParam Map<String, Object> paths,
  23. @HeaderParam Map<String, Object> headers,
  24. @BodyParam("form") Map<String, Object> body, Callback callback);
  25. @HttpMethod(HttpMethodEnum.POST)
  26. @Path("/idl_baidu/faceverifyservice/face_deleteuser")
  27. @Serialization(SerializationEnum.JSON)
  28. public abstract Map<String, Object> face_deleteuser(
  29. @QueryParam Map<String, Object> querys,
  30. @PathParam Map<String, Object> paths,
  31. @HeaderParam Map<String, Object> headers,
  32. @BodyParam("json") Map<String, Object> body, Callback callback);
  33. }

接下来需要在业务逻辑代码中引用这个服务,见DemoService.java

  1. public class DemoService extends HttpServiceBase {
  2. @HttpService
  3. private BaiduHttpService service;
  4. public DemoService() {
  5. super();
  6. }
  7. public Map<String, Object> get() {
  8. Map<String, Object> querys = Maps.newHashMap();
  9. Map<String, Object> paths = Maps.newHashMap();
  10. Map<String, Object> headers = Maps.newHashMap();
  11. querys.put("cityname", "杭州");
  12. headers.put("apikey", "31da01966ee96d06fdd5c2f2c855424e");
  13. paths.put("pathId", "weatherservice");
  14. // Map<String, Object> result = service.listcity(querys, paths,
  15. // headers);
  16. Map<String, Object> result = service.listcity2(querys, paths, headers,
  17. new Callback() {
  18. @Override
  19. public void execute(Map<String, Object> querys,
  20. Map<String, Object> paths,
  21. Map<String, Object> headers,
  22. Map<String, Object> body) {
  23. System.out.println(querys);
  24. querys.put("aa", "bb");
  25. }
  26. });
  27. return result;
  28. }
  29. public Map<String, Object> post() {
  30. Map<String, Object> querys = Maps.newHashMap();
  31. Map<String, Object> paths = Maps.newHashMap();
  32. Map<String, Object> headers = Maps.newHashMap();
  33. Map<String, Object> body = Maps.newHashMap();
  34. headers.put("apikey", "31da01966ee96d06fdd5c2f2c855424e");
  35. body.put("fromdevice", "pc");
  36. body.put("clientip", "10.10.10.0");
  37. body.put("detecttype", "LocateRecognize");
  38. body.put("languagetype", "CHN_ENG");
  39. body.put("imagetype", "1");
  40. body.put(
  41. "image",
  42. "/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDABMNDxEPDBMREBEWFRMXHTAfHRsbHTsqLSMwRj5KSUU+RENNV29eTVJpU0NEYYRiaXN3fX59S12Jkoh5kW96fXj/2wBDARUWFh0ZHTkfHzl4UERQeHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHj/wAARCAAfACEDAREAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAQDBQb/xAAjEAACAgICAgEFAAAAAAAAAAABAgADBBESIRMxBSIyQXGB/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/APawEBAQEBAgy8i8ZTVV3UY6V1eU2XoWDDZB19S646Gz39w9fkKsW1r8Wm2yo1PYis1be0JG9H9QNYCAgc35Cl3yuVuJZl0cB41rZQa32dt2y6OuOiOxo61vsLcVblxaVyXD3hFFjL6La7I/sDWAgICAgICB/9k=");
  43. Map<String, Object> result = service.ocr(querys, paths, headers, body,
  44. new Callback() {
  45. @Override
  46. public void execute(Map<String, Object> querys,
  47. Map<String, Object> paths,
  48. Map<String, Object> headers,
  49. Map<String, Object> body) {
  50. }
  51. });
  52. return result;
  53. }
  54. public Map<String, Object> postJson() {
  55. Map<String, Object> querys = Maps.newHashMap();
  56. Map<String, Object> paths = Maps.newHashMap();
  57. Map<String, Object> headers = Maps.newHashMap();
  58. Map<String, Object> body = Maps.newHashMap();
  59. headers.put("apikey", "31da01966ee96d06fdd5c2f2c855424e");
  60. FaceDeleteUserModel param = new FaceDeleteUserModel();
  61. param.setJsonrpc("2.0");
  62. param.setMethod("Delete");
  63. param.setId(12);
  64. List<FaceDeleteUserInternalModel> paramList = Lists.newArrayList();
  65. FaceDeleteUserInternalModel innerMap = new FaceDeleteUserInternalModel();
  66. innerMap.setVersionnum("1.0.0.1");
  67. innerMap.setGroupid("0");
  68. innerMap.setType("st_groupverify");
  69. innerMap.setClientip("10.23.34.5");
  70. innerMap.setAppid("31da01966ee96d06fdd5c2f2c855424e");
  71. innerMap.setLogid("12345");
  72. innerMap.setCmdid("1000");
  73. innerMap.setUsername("test");
  74. paramList.add(innerMap);
  75. param.setParams(paramList);
  76. body.put("anykey", param);
  77. Map<String, Object> result = service.face_deleteuser(querys, paths,
  78. headers, body, new Callback() {
  79. @Override
  80. public void execute(Map<String, Object> querys,
  81. Map<String, Object> paths,
  82. Map<String, Object> headers,
  83. Map<String, Object> body) {
  84. }
  85. });
  86. return result;
  87. }
  88. }

注解说明:

Annotation Target Spec
Scheme TYPE \ METHOD 默认是http,表示服务协议(一般都是http或者https)
Host TYPE \ METHOD 默认是空,服务的host,http://xxx.xx.xx/中的x部分
HttpMethod METHOD 默认是HttpMethodEnum.GET 表示http服务的方法类型支持GET\POST\PUT\DELETE
Path TYPE \ METHOD 默认是空,表示服务的路径,http://xxx.xx.xx/yy/zz/中的y和z部分
Serialization TYPE \ METHOD 默认是SerializationEnum.STRING,表示对于服务响应内容的序列化方式,支持json和xml还有普通字符串
QueryParam PARAMETER 表示http服务的query,http://xxx.xx.xx/yy/zz?q=aaa&p=bbb中?后面的部分,该注解标注在Map类型的对象上,所有的query都被转为key-value
PathParam PARAMETER 表示http服务的path替换部分,http://xxx.xx.xx/{yy}/zz?q=aaa&p=bbb中{yy}的部分,该注解标注在Map类型的对象上,Map中的key-value会替换{yy}
HeaderParam PARAMETER 表示http服务的header部分,该注解标注在Map类型的对象上,Map中的key-value表示header的key-value
BodyParam PARAMETER 默认值是form,表示http服务post时的body部分,该注解标注在Map类型的对象上,Map中的key-value表示FORM表单的kv部分或者是json格式的内容,json时不关心key是什么

改进中

1, 对于QueryParam等的限制放开,支持自定义业务对象。

github地址

https://github.com/changedi/http-funny

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注