每天学习一点点 编程PDF电子书、视频教程免费下载:
http://www.shitanlife.com/code

spring web 项目提供的RestTemplate,使java访问url更方便,更优雅。

它是spring提供的异步的客户端http访问的核心class,它提供非常简单的RESTful方式与http server端进行数据交互,根据所提动的URLs进行http访问,并处理返回结果。它是基于JDK HTTP connection建立的。因此他可以使用不同的HTTP库(apache,netty and OkHttp)来setRequestFactory。

它实现了以下6个主要的HTTP meshod:

HTTP method RestTemplate methods
DELETE delete
GET getForObject,getForEntity
HEAD headForHeaders
OPTIONS optionsForAllow
PUT put
any exchange,execute

现简单介绍在Springboot中使用RestTemplate

首先在代码中加入RestTemplate的配置类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate; /**
* Created by liuxu on 2017/12/22.
* RestTemplate配置类
*/
@Configuration
public class RestTemplateConfig { @Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory){
return new RestTemplate(factory);
} @Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(5000);//单位为ms
factory.setConnectTimeout(5000);//单位为ms
return factory;
}
}

然后在需要访问url的类中注入RestTemplate

@Autowired
private RestTemplate restTemplate;

使用RestTemplate发送get请求

//get json数据
JSONObject json = restTemplate.getForEntity(url, JSONObject.class).getBody();

使用RestTemplate发送post请求

//post json数据
JSONObject postData = new JSONObject();
postData.put("data", "request for post");
JSONObject json = restTemplate.postForEntity(url, postData, JSONObject.class).getBody(); //对基本类型的解析 JSONObject obj =
json ;
System.out.println("name:" + obj.getString("name")); 
System.out.println("sex:" + obj.getString("sex"));
System.out.println("age" + obj.getInt("age"));
System.out.println("is_student" + obj.getBoolean("is_student"));
//对数组的解析
JSONArray hobbies = obj.getJSONArray("hobbies");
System.out.println("hobbies:");
for (int i = 0; i < hobbies.length(); i++) { String s = (String) hobbies.get(i); System.out.println(s); }

设置请求头

//post json string data
//return string
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
headers.setContentType(type);
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
JSONObject jsonObj = JSONObject.parseObject(paras);
HttpEntity<String> formEntity = new HttpEntity<String>(jsonObj.toString(), headers);
String result = restTemplate.postForObject(url, formEntity, String.class);

每天学习一点点 编程PDF电子书、视频教程免费下载:
http://www.shitanlife.com/code

Springboot 使用 RestTemplate的更多相关文章

  1. SpringBoot系列: RestTemplate 快速入门

    ====================================相关的文章====================================SpringBoot系列: 与Spring R ...

  2. SpringBoot 使用 RestTemplate 调用exchange方法 显示错误信息

    SpringBoot使用RestTempate SpringBoot使用RestTemplate摘要认证 SpringBoot使用RestTemplate基础认证 SpringBoot使用RestTe ...

  3. SpringBoot使用RestTemplate基础认证

    SpringBoot使用RestTempate SpringBoot使用RestTemplate摘要认证 SpringBoot使用RestTemplate基础认证 SpringBoot使用RestTe ...

  4. SpringBoot使用RestTemplate 摘要认证

    SpringBoot使用RestTempate SpringBoot使用RestTemplate摘要认证 SpringBoot使用RestTemplate基础认证 SpringBoot使用RestTe ...

  5. SpringBoot使用RestTemplate

    SpringBoot使用RestTempate SpringBoot使用RestTemplate摘要认证 SpringBoot使用RestTemplate基础认证 设置pom引用 <?xml v ...

  6. SpringBoot配置RestTemplate的代理和超时时间

    application.properties: #代理设置 proxy.enabled=false proxy.host=192.168.18.233 proxy.port=8888 #REST超时配 ...

  7. springboot使用RestTemplate以post方式发送json字符串参数(以向钉钉机器人发送消息为例)

    使用springboot之前,我们发送http消息是这么实现的 我们用了一个过时的类,虽然感觉有些不爽,但是出于一些原因,一直也没有做处理,最近公司项目框架改为了springboot,springbo ...

  8. SpringBoot集成RestTemplate

    先把原文列出来: springboot实战之常用http客户端整合 springboot2.0集成RestTemplate -----------开始------------ SpringBoot应用 ...

  9. springboot系列十二、springboot集成RestTemplate及常见用法

    一.背景介绍 在微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.我们可以使用JDK原生的URLConnection.Apache的Http Client.N ...

随机推荐

  1. [JZOJ5984] 仙人掌

    Description Solution 标算我并不会... 考虑一种根号思想 首先以 \(1\) 为根dfs整棵树 那么在任意时刻一个点的儿子的权值种类最多只会有 \(\sqrt m\) 种. 可以 ...

  2. Hyperledger Fabric密码模块系列之BCCSP(一)

    Fabric作为IBM主导的区块链平台,可谓是联盟链中的一枝独秀,现如今已经有100多个大型国际银行.金融以及科技公司的加盟.与其说Fabric是区块链的一种平台,倒不如说是一个区块链框架更加精确,因 ...

  3. Maven deploy 部署 jar+pom 到 Nexus 私服

    经验之谈 工作中,我们常常需要将基础架构部门的 jar 包提供给业务部门的同事使用,那么,需要将 jar 包 deploy 到 nexus 私服上,网上资料不是很多,这里说一下具体细节. 首先,是打 ...

  4. 定义对象为什不可以写到while语句外面。VS2017

    /// <summary> /// 绑定产品信息到网络列表 /// </summary> private void BindProduct() { Pros = new Lis ...

  5. Linux常用基本命令:grep-从文件或者管道中筛选匹配的行

    grep命令 作用:从文本文件或管道数据流中筛选匹配的行及数据,配合正则表达式一起使用,功能更加强大. 格式: grep [options] [pattern] [file] 1,匹配包含" ...

  6. HDU6191(01字典树启发式合并)

    Query on A Tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Othe ...

  7. 初学HTML-5

    表格标签:用来给一堆数据添加表格语义. 格式:<table> <tr> <td></td> </tr> </table> tab ...

  8. 教你读懂vue源码技术教程

    由于 Vue 的源码采用 ES6,所以你至少应该掌握 ES6 才能看得懂,其次你最好对 package.json 中的字段的作用有所了解.由于 Vue 使用 Rollup 构建,所以你不了解 Roll ...

  9. python之编码和解码

    编码: 1. ascii. 有: 数字, 字母, 特殊字符. 8bit 1byte 128 最前面是0 2. gbk. 包含: ascii, 中文(主要), 日文, 韩文, 繁体文字. 16bit, ...

  10. 转:drupal常用api

    drupal常用api   最短的函数 // 语言字串,除了可以获取对应语言外,还可以设置字串变量.可以是!var, @var或 %var,%var就添加元素外层.@var会过滤HTML,!var会原 ...