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. mongodb 3.2 yaml 配置详解及范例

    mongodb3.x版本后就是要yaml语法格式的配置文件,下面是yaml配置文件格式如下:官方yaml配置文件选项参考:https://docs.mongodb.org/manual/ ... #c ...

  2. asp.net去除HTML标签

    public string NoHTML(string Htmlstring) //替换HTML标记 { //删除脚本 Htmlstring = Regex.Replace(Htmlstring, @ ...

  3. 【案例】鼠标按下,DIV跟随移动

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 记录openSUSE 源码安装node.js

    openSUSE版本: 42.2 目标:安装好 Node.js v6.10.3 在终端中可以使用 "su" 命令,切换到root用户. 1. 安装 gcc,gcc-c++ zypp ...

  5. unordered_map

    #include <iostream> #include <cstdio> #include <queue> #include <algorithm> ...

  6. 扩展kmp板子

    using namespace std; #include <cstdio> #include <cstring> #include <algorithm> #de ...

  7. 莫烦pytorch学习笔记(七)——Optimizer优化器

    各种优化器的比较 莫烦的对各种优化通俗理解的视频 import torch import torch.utils.data as Data import torch.nn.functional as ...

  8. VS2010文件包含

    一. 关于iostream.h VS2010的iostream不加.h在后面(加.h的是旧版本),把iostream作为类,正确使用方法: #include<iostream> using ...

  9. Java-MyBatis-MyBatis3-XML映射文件:select

    ylbtech-Java-MyBatis-MyBatis3-XML映射文件:select 1.返回顶部 1. select 查询语句是 MyBatis 中最常用的元素之一,光能把数据存到数据库中价值并 ...

  10. Linux 的文件和目录管理类命令

    文件和目录管理类的命令 目录: cd ~[username] ~:指定用户的家目录 cd -:回到上一次所在的目录 路径: 相对路径中: .:当前目录 ..:父目录 pwd: print workin ...