Spring的RestTemplate及大地简化了REST Client的开发,但每次还要编写大量的模板代码,代码不够简洁。我对他进行了一次包装,采用接口来声明REST接口,使用Annotation对interface的方法进行标注。如下声明一个REST接口

//接口必须继承BaseRestClient,提供了一个setUrl的基本方法。
public interface ITestRest extends BaseRestClient
{
//声明一个REST方法,method是GET,在路径里面有个参数id,如: http://localhost:8080/get/{id}。返回一个UserInfo对象,由Json反射过来。
@RestClient(method = HttpMethod.GET)
UserInfo getUser(@PathParam(value = "id") String id);
//声明一个REST方法,method用POST,除了路径里面的id,还有一个表单
@RestClient(method = HttpMethod.POST)
UserInfo postUser(@PathParam(value = "id") String id,@FormBody UserForm form);
//表单中含有文件
@RestClient(method = HttpMethod.POST,hasFile = true)
UserInfo postUserWithFile(@PathParam(value = "id") String id,@FormBody UserFormWithFile form);
}

声明Bean

@Bean
public ITestRest testRest(RestTemplate restTemplate){
ITestRest testRest= RestClientBuilder.newRestClient(ITestRest.class,restTemplate);
return testRest;
}
@Bean
public RestTemplate restTemplate(){
return new RestTemplateBuilder()
.additionalMessageConverters(new MappingJackson2HttpMessageConverter())
.additionalMessageConverters(new FormHttpMessageConverter()).build();
}

调用方

    @Autowired
ITestRest testRest;
......
testRest.setUrl("http://localhost:8080/get/{id}");
UserInfo user=testRest.getUser("123456");

由于访问路径可能会变化,比如采用了集群,所以在调用前需要set一下,url放到ThreadLocal里面,线程安全。 
如果不变,可以在@RestClient声明中加上path指定访问地址 
github地址:https://github.com/bobdeng/ssrf

对Spring 的RestTemplate进行包装的更多相关文章

  1. Spring’s RestTemplate

    Spring’s RestTemplate /** * After the word document is generated in memory we can upload it to the s ...

  2. SpringBoot+RestTemplate 简单包装

        RestTemplate设计是为了Spring更好的请求并解析Restful风格的接口返回值而设计的,通过这个类可以在请求接口时直接解析对应的类.     在SpringBoot中对这个类进行 ...

  3. spring的RestTemplate使用指南

    前言:现在restful接口越来越广泛,而如今很多接口摒弃了传统的配置复杂的webService开发模式,在java领域只需要很简单的springMvc就可以声明为一个控制器,再加上service层, ...

  4. 还不知道spring的RestTemplate的妙用吗

    为什么要使用RestTemplate? 随着微服务的广泛使用,在实际的开发中,客户端代码中调用RESTful接口也越来越常见.在系统的遗留代码中,你可能会看见有一些代码是使用HttpURLConnec ...

  5. Spring的RestTemplate

    Spring提供了一个RestTemplate模板工具类,对基于Http的客户端进行了封装,并且实现了对象与json的序列化和反序列化,非常方便.RestTemplate并没有限定Http的客户端类型 ...

  6. Spring中RestTemplate进行Http调用

    Spring中的RestTemplate类源自spring-web,http调用中设置超时时间.设置连接池管理等非常重要,保证了系统的可用性,避免了长时间连接不上或者等待数据返回,拖垮系统. 现贴出工 ...

  7. Spring中RestTemplate的使用方法

    一.REST 在互联网中,我们会通过请求url来对网络上的资源做增删改查等动作,这里的请求包含两部分:动词,主要包括增.删.改.查:名词,就是网络中的各种资源.传统的非REST风格的请求方式是把动词和 ...

  8. Spring boot ----RestTemplate学习笔记

    ****spring boot-----restTemplate 封装了HttpURLConnection,HttpClient,Netty等接口访问实现库 restTemplet包含以下部分 Htt ...

  9. 使用Spring的RestTemplate进行接口调用

    引自:http://www.zimug.com/ 1.常见的http服务的通信方式 经常使用的方式有HttpClient.OkHttp.RestTemplate.其中RestTemplate是一种更优 ...

随机推荐

  1. django和celery结合应用

    django+celery项目结构 - project_name - app01 - __init__.py - admin.py - views.py - modes.py - tasks.py # ...

  2. C++回顾day02---<继承相关问题>

    一:继承和组合混搭时,构造和析构调用原则 (一)先构造父类,再构造成员变量,最后构造自己 (二)先析构自己,再析构成员变量,最后父类析构(方向与构造相反) class A { public: int ...

  3. linux,pthread(转)

    互斥量.条件变量与pthread_cond_wait()函数的使用,详解(二)   1.Linux“线程” 进程与线程之间是有区别的,不过linux内核只提供了轻量进程的支持,未实现线程模型.Linu ...

  4. LFYZ-OJ ID: 1020 过河卒(NOIP2002)

    过河卒 Proble Description 如图,A 点有一个过河卒,需要走到目标 B 点.卒行走规则:可以向下.或者向右.同时在棋盘上的任一点有一个对方的马(如上图的C点),该马所在的点和所有跳跃 ...

  5. 使用PHP做分页查询(查询结果也显示为分页)

    1.先把数据库里所有的数据分页显示在页面,并在显示数据的表格上方加上查询表单.(加上条件,实现目标结果.) <!DOCTYPE html PUBLIC "-//W3C//DTD XHT ...

  6. Linux负载查询定位工具

    1 uptime命令,负载查询命令 02:34:03 // 当前时间up 2 days, 20:14 // 系统运行时间1 user // 正在登录用户数 而最后三个数字呢,依次则是过去 1 分钟.5 ...

  7. Alpha 冲刺 (8/10)

    目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:冲刺倒计时之8 团队部分 后敬甲(组长) 过去两天完成了哪些任务 首页重新设计 课程时间线确定 答辩准备 接下来的计划 ...

  8. 【easy】234. Palindrome Linked List

    ques: 判断一个链表是否回文 Could you do it in O(n) time and O(1) space? method:先将链表分为两部分,将后半部分反转,最后从前往后判断是否相等. ...

  9. Linux 首先基本包安装(vim啊什么的),源,源优化,项目架构介绍, (LNMuWsgi)Django项目相关软件mysql,redies,python(相关模块)安装配置测试

    内容 补充: 查看已启动服务的端口 netstat -tulnp |grep (方式1) ss -tulnp|grep (方式2) 前期铺垫: . Linux要能上网 . 掌握Linux软件包安装方法 ...

  10. js中setTimeout() 时间参数为0

    当看到下面 这种setTimeout 设置为0 写法的时候一脸懵逼,完全没用过. var fuc = [1,2,3]; for(var i in fuc){ setTimeout(function() ...