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. 2017-12-19python全栈9期第四天第一节之昨日内容回顾与作业讲解之插入insert和extend

    #!/user/bin/python# -*- coding:utf-8 -*-li = ['zs','ls','ww','zl']li.insert(4,'cc')print(li)li.exten ...

  2. django - 总结 - form表单

    {{ form.as_table }} 以表格的形式将它们渲染在<tr> 标签中 {{ form.as_p }} 将它们渲染在<p> 标签中 {{ form.as_ul }} ...

  3. [物理学与PDEs]第5章第2节 变形的描述, 应变张量 2.1 变形梯度张量

    $$\bex \rd{\bf y}={\bf F}\rd {\bf x}, \eex$$ 其中 ${\bf F}=\n_x{\bf y}=\sex{\cfrac{\p y_i}{\p x_j}}$ 为 ...

  4. day 14 - 2 生成器练习

    相关练习 1.处理文件,用户指定要查找的文件和内容,将文件中包含要查找内容的每一行都输出到屏幕 #比较 low 的方法 def check_file(filename,aim): with open( ...

  5. 忘记mysql的登陆密码该怎么办?

    1.如果忘记了其他用户的密码,可以使用root账户进入mysql,修改mysql.user表中的用户密码 2.如果忘记了root的mysql密码,可以使用如下方式: 确认服务器处于安全的状态,也就是没 ...

  6. Spring Cloud 2-Hystrix 断路容错保护(四)

    Spring Cloud  Hystrix  1.RestTemplate 容错 pom.xml application.yml application.java HelloService.java ...

  7. pt-online-schema-change VS oak-online-alter-table【转】

    前言 在上篇文章中提到了MySQL 5.6 Online DDL,如果是MySQL 5.5的版本在DDL方面是要付出代价的,虽然已经有了Fast index Creation,但是在添加字段还是会锁表 ...

  8. C#常用的单元测试框架

    C#常用的单元测试框架有XUnit .NUnit .MSTest 做过单元测试的同学大概都知道以上几种测试框架,这几种框架除了标注测试类和方法的特性用的不一样之外,XUnit 和 NUnit 是非常相 ...

  9. vue的配置环境篇

    1.电脑已经安装的nodejs和webpack. 2.1)打开cmd.win+r.可以直接输入node -v查看版本.安装淘宝镜像  npm install -g cnpm --registry=ht ...

  10. ECMAScript 6中数组新方法

    数组的方法 数组的的大部分方法都可以实现数组的遍历. foreach方法 实现数组的遍历 const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; arr.forEach(fu ...