TZ_12_Spring的RestTemplate
1.Http客户端工具
HttpClient:HttpClient是Apache公司的产品,是Http Components下的一个组件。
特点:
基于标准、纯净的Java语言。实现了Http1.0和Http1.1
以可扩展的面向对象的结构实现了Http全部的方法(GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE)
支持HTTPS协议。
通过Http代理建立透明的连接。
- 自动处理Set-Cookie中的Cookie。
2这个接口返回一个User对象,但我们实际的到是一个User的Json字符串需要我,们手动的转为User对象 此时就可以借助Spring的RestTemplate
- @Test
- public void testGetPojo() throws IOException {
- HttpGet request = new HttpGet("http://localhost/hello");
- String response = this.httpClient.execute(request, new BasicResponseHandler());
- System.out.println(response);
- }
3.Spring的RestTemplate
Spring提供了一个RestTemplate模板工具类,对基于Http的客户端进行了封装,并且实现了对象与json的序列化和反序列化,非常方便。RestTemplate并没有限定Http的客户端类型,而是进行了抽象,目前常用的3种都有支持:
1>HttpClient
2>OkHttp
3>JDK原生的URLConnection(默认的)
4.在启动类位置注册:
- @Bean
- public RestTemplate restTemplate() {
- return new RestTemplate();
- }
在测试类中直接@Autowired
注入:
- @RunWith(SpringRunner.class)
- @SpringBootTest(classes = HttpDemoApplication.class)
- public class HttpDemoApplicationTests {
- @Autowired
- private RestTemplate restTemplate;
- @Test
- public void httpGet() {
- User user = this.restTemplate.getForObject("http://localhost:8080/User/1.html", User.class);
- System.out.println(user);
- }
- }
TZ_12_Spring的RestTemplate的更多相关文章
- HttpClient的替代者 - RestTemplate
需要的包 ,除了Spring的基础包外还用到json的包,这里的数据传输使用json格式 客户端和服务端都用到一下的包 <!-- Spring --> <dependency> ...
- RestTemplate发送请求并携带header信息
1.使用restTemplate的postForObject方法 注:目前没有发现发送携带header信息的getForObject方法. HttpHeaders headers = new Http ...
- 【Spring-web】RestTemplate源码学习——梳理内部实现过程
2016-12-28 by 安静的下雪天 http://www.cnblogs.com/quiet-snowy-day/p/6228198.html 提示:使用手机浏览时请注意,图多费流量. 本篇 ...
- 【Spring-web】RestTemplate源码学习
2016-12-22 by 安静的下雪天 http://www.cnblogs.com/quiet-snowy-day/p/6210288.html 前言 在Web开发工作中,有一部分开发任务 ...
- RestTemplate配置
什么是RestTemplate? RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效 ...
- 使用RestTemplate发送post请求
最近使用RestTemplate发送post请求,遇到了很多问题,如转换httpMessage失败,中文乱码等,调了好久才找到下面较为简便的方法: RestTemplate restTemplate ...
- Spring Boot+Cloud RestTemplate 调用IP或域名
在SpringBoot+Cloud的项目中,我们使用了自动配置的OAuth2RestTemplate,RestTemplate,但是在使用这些restTemplate的时候,url必须是服务的名称,如 ...
- Spring RestTemplate: 比httpClient更优雅的Restful URL访问, java HttpPost with header
{ "Author": "tomcat and jerry", "url":"http://www.cnblogs.com/tom ...
- RestTemplate实践
什么是RestTemplate? RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效 ...
随机推荐
- cocos2dx触摸响应
Layer其实继承了触控的接口. 所以只需要重写一些函数即可. 在helloword类中重写: virtual bool init(); /** Callback functi ...
- vagrant生成多台虚拟机
第一种: # -*- mode: ruby -*- # vi: set ft=ruby : # All Vagrant configuration is done below. The "2 ...
- JS规则 我还有其它用途( +号操作符)例如,算术操作符(+、-、*、/等),比较操作符(<、>、>=、<=等),逻辑操作符(&&、||、!)
我还有其它用途( +号操作符) 操作符是用于在JavaScript中指定一定动作的符号. (1)操作符 看下面这段JavaScript代码. sum = numa + numb; 其中的"= ...
- LoadRunner例子:检查点为参数的一个例子
LoadRunner例子:检查点为参数的一个例子 检查点是LoadRunner的一个功能,用来验证业务功能的正确性.如果检查的内容是变化的,脚本该如何写呢? 问题提出:LoadRunner订票网站例子 ...
- LoadRunner函数的介绍
LoadRunner函数的介绍 LoadRunner函数 一:通用函数 LoadRunner的通用函数以lr为前缀,可以在任何协议中使用.可以如下分类: 信息相关的函数: lr_error_messa ...
- Jmeter教程 简单的压力测试【转】
Jmeter教程 简单的压力测试[转] Jmeter是一个非常好用的压力测试工具. Jmeter用来做轻量级的压力测试,非常合适,只需要十几分钟,就能把压力测试需要的脚本写好. 阅读目录 什么是压力 ...
- python 递归计算若干工作日后的日期
import datetime # 根据第一次计算出来的休息日数,计算还需要的工作日数.(递归调用) def get_next_date(self, start_date, weekend_days) ...
- leetcode146周赛-1131-绝对值表达式的最大值
题目描述: class Solution: def maxAbsValExpr(self, arr1, arr2) -> int: def function(s1,s2): result1=[] ...
- leetcode-122-买卖股票的最佳时机②
题目描述: 方法一: class Solution: def maxProfit(self, prices: List[int]) -> int: profit = 0 for i in ran ...
- php中$_REQUEST、 $_GET、 $_POST、 $_COOKIE 的关系和区别
看到REQUEST可以通吃GET .POST .COOKIE 后 感觉这个$_REQUEST太强大了是不是其他的几个超级变量就没有用了,下面对他们整体做个比较: 1.安全性 post>get 2 ...