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的更多相关文章

  1. HttpClient的替代者 - RestTemplate

    需要的包 ,除了Spring的基础包外还用到json的包,这里的数据传输使用json格式 客户端和服务端都用到一下的包 <!-- Spring --> <dependency> ...

  2. RestTemplate发送请求并携带header信息

    1.使用restTemplate的postForObject方法 注:目前没有发现发送携带header信息的getForObject方法. HttpHeaders headers = new Http ...

  3. 【Spring-web】RestTemplate源码学习——梳理内部实现过程

    2016-12-28 by 安静的下雪天  http://www.cnblogs.com/quiet-snowy-day/p/6228198.html  提示:使用手机浏览时请注意,图多费流量. 本篇 ...

  4. 【Spring-web】RestTemplate源码学习

     2016-12-22   by 安静的下雪天  http://www.cnblogs.com/quiet-snowy-day/p/6210288.html 前言 在Web开发工作中,有一部分开发任务 ...

  5. RestTemplate配置

    什么是RestTemplate? RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效 ...

  6. 使用RestTemplate发送post请求

    最近使用RestTemplate发送post请求,遇到了很多问题,如转换httpMessage失败,中文乱码等,调了好久才找到下面较为简便的方法: RestTemplate restTemplate ...

  7. Spring Boot+Cloud RestTemplate 调用IP或域名

    在SpringBoot+Cloud的项目中,我们使用了自动配置的OAuth2RestTemplate,RestTemplate,但是在使用这些restTemplate的时候,url必须是服务的名称,如 ...

  8. Spring RestTemplate: 比httpClient更优雅的Restful URL访问, java HttpPost with header

    { "Author": "tomcat and jerry", "url":"http://www.cnblogs.com/tom ...

  9. RestTemplate实践

    什么是RestTemplate? RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效 ...

随机推荐

  1. iOS逆向系列-逆向APP思路

    界面分析 通过Cycript.Reveal. 对于Reveal安装配置可参考配置iOS逆向系列-Reveal 通过Reveal找到内存中的UI对象 静态分析 开发者编写的所有代码最终编译链接到Mach ...

  2. SpringCloud学习笔记《---02 Eureka ---》篇

  3. JS流程控制语句 多种选择(Switch语句) 当有很多种选项的时候,switch比if else使用更方便。

    多种选择(Switch语句) 当有很多种选项的时候,switch比if else使用更方便. 语法: switch(表达式) { case值1: 执行代码块 1 break; case值2: 执行代码 ...

  4. Leetcode187. Repeated DNA Sequences重复的DNA序列

    所有 DNA 由一系列缩写为 A,C,G 和 T 的核苷酸组成,例如:"ACGAATTCCG".在研究 DNA 时,识别 DNA 中的重复序列有时会对研究非常有帮助. 编写一个函数 ...

  5. AM运行中的垃圾数据清理

    1.下载 filetmpclear.bat 2.打开 这个批处理文件,修改参数 3.按下图 第一个红色框内, 地址: 可以在  AM8服务管理器- 文件服务 ,数据路径后再加  \__Temp__ 第 ...

  6. JedisCluster API 整理

    windows版redis启动服务器命令:redis-server redis.windows.conf 图表来自菜鸟教程: 列表的操作命令 序号 命令及描述 1 BLPOP key1 [key2 ] ...

  7. MyEclipse如何使用debug模式

    知道如何打断点,如何进入debug与debug模式的视图,还有工具栏的使用和快捷键的使用 https://blog.csdn.net/menglanyingfei/article/details/55 ...

  8. 0903NOIP模拟测试赛后总结

    分-rank33.这次考试心态挂了. 拿到题目通读三道题,发现都十分恶心. 然后把时间押到了T1上.将近两个小时,打了个dfs,一直调调调. 最后没调出来,手模了个数据就把自己两个小时的思路hack了 ...

  9. 使用 SourceTree 操作时弹出 password required

    通过 https 的方式克隆仓库的,SourceTree 推送等操作的时候会弹出提示要求输入密码. 在仓库里面设置: 远程仓库-选中仓库-点击编辑-修改 url 路径, 路径格式,以码云为例: htt ...

  10. System.Web.Mvc.HttpUnauthorizedResult.cs

    ylbtech-System.Web.Mvc.HttpUnauthorizedResult.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neut ...