Springboot 使用 RestTemplate
每天学习一点点 编程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的更多相关文章
- SpringBoot系列: RestTemplate 快速入门
====================================相关的文章====================================SpringBoot系列: 与Spring R ...
- SpringBoot 使用 RestTemplate 调用exchange方法 显示错误信息
SpringBoot使用RestTempate SpringBoot使用RestTemplate摘要认证 SpringBoot使用RestTemplate基础认证 SpringBoot使用RestTe ...
- SpringBoot使用RestTemplate基础认证
SpringBoot使用RestTempate SpringBoot使用RestTemplate摘要认证 SpringBoot使用RestTemplate基础认证 SpringBoot使用RestTe ...
- SpringBoot使用RestTemplate 摘要认证
SpringBoot使用RestTempate SpringBoot使用RestTemplate摘要认证 SpringBoot使用RestTemplate基础认证 SpringBoot使用RestTe ...
- SpringBoot使用RestTemplate
SpringBoot使用RestTempate SpringBoot使用RestTemplate摘要认证 SpringBoot使用RestTemplate基础认证 设置pom引用 <?xml v ...
- SpringBoot配置RestTemplate的代理和超时时间
application.properties: #代理设置 proxy.enabled=false proxy.host=192.168.18.233 proxy.port=8888 #REST超时配 ...
- springboot使用RestTemplate以post方式发送json字符串参数(以向钉钉机器人发送消息为例)
使用springboot之前,我们发送http消息是这么实现的 我们用了一个过时的类,虽然感觉有些不爽,但是出于一些原因,一直也没有做处理,最近公司项目框架改为了springboot,springbo ...
- SpringBoot集成RestTemplate
先把原文列出来: springboot实战之常用http客户端整合 springboot2.0集成RestTemplate -----------开始------------ SpringBoot应用 ...
- springboot系列十二、springboot集成RestTemplate及常见用法
一.背景介绍 在微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.我们可以使用JDK原生的URLConnection.Apache的Http Client.N ...
随机推荐
- K临近算法
K临近算法原理 K临近算法(K-Nearest Neighbor, KNN)是最简单的监督学习分类算法之一.(有之一吗?) 对于一个应用样本点,K临近算法寻找距它最近的k个训练样本点即K个Neares ...
- cJSON填坑记
1. 艰辛的过程 最近做了一个嵌入式的项目,需要与服务器进行连接.为了方便服务器处理数据,经商定后统一采用JSON形式进行数据的传输. 以前没有用过JSON格式进行数据处理,所以上网搜索了一下,很多人 ...
- 通过webservice(System.Data.OracleClient)调试oracle
环境:vs2008+webservice+net framework3.5+oracle10g 原因:在项目中运行web程序,默认是使用vs内置web服务器(develop server),而这个内置 ...
- [转]ionic3项目实战教程三(创建provider、http请求、图文列表、滑动列表)
本文转自:https://blog.csdn.net/lyt_angularjs/article/details/81145468 版权声明:本文为博主原创文章,转载请注明出处.谢谢! https:/ ...
- www.jqhtml.com 前端框架特效
www.jqhtml.com * 请选择课程 初级班 (PS.HTML.CSS.静态网站项目实战) 中级班 JavaScript基础.JavaScript DOM.jQuery.JS进阶.HTML5和 ...
- httpclient请求服务的各种方法实例
<!--话不多说,直接上代码--> import com.csis.ConfigManagerimport com.csis.io.web.DefaultConfigItemimport ...
- 【高并发解决方案】7、一致性hash解读
一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法,设计目标是为了解决因特网中的热点(Hot spot)问题,初衷和CARP十分类似.一致性哈希修正了CARP使用的简 ...
- csharp: read system DSN configured get Driver Names on windows
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- idea not found for the web module
intellij IDEA 的tomcat 配置项里面没有的app web:war exploded 没有配置/路径
- 洛谷P4007 小 Y 和恐怖的奴隶主(期望dp 矩阵乘法)
题意 题目链接 Sol 首先不难想到一种暴力dp,设\(f[i][a][b][c]\)表示还有\(i\)轮没打,场上有\(a\)个1血,\(b\)个2血,\(c\)个三血 发现状态数只有\(s = 1 ...