RestTemplate的GET与POST
- 发送GET请求:
//设置请求头
HttpHeaders headers = new HttpHeaders();
headers.add("token",PostUtils.getToken()); //调用接口所需token,如果项目中没加权限拦截可以不需要这个token设置
//请求体
HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
//发起请求
RestTemplate restTemplate=new RestTemplate();
ResponseEntity<JSONObject> jsonObjectResponseEntity = restTemplate.exchange("调用的接口地址", HttpMethod.GET, requestEntity,JSONObject.class);
//将拿到的数据转换成自己想要的格式
ResponseLockList responseLockList=JSON.parseObject(jsonObjectResponseEntity.getBody().toString(), new TypeReference<ResponseLockList>() {});- 发送POST请求:
//入参
JSONObject jsonObj=null;
//设置请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8); //请求的入参方式是json
headers.add("token", getToken()); //调用接口所需token,如果项目中没有权限拦截可以不需要这个token设置
//请求体
HttpEntity<String> formEntity = new HttpEntity<String>(jsonObj.toString(), headers);
//发送请求
RestTemplate restTemplate = new RestTemplate();
//返回的json字符串
String json=restTemplate.postForObject("调用的接口地址", formEntity, String.class);
//将json字符串转换成对象
ResponseSaasDel pwd=JSON.parseObject(json, new TypeReference<ResponseSaasDel>() {});- 原文链接:https://blog.csdn.net/likekobe2012/article/details/82663725
RestTemplate的GET与POST的更多相关文章
- 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服务的方法,能够大大提高客户端的编写效 ...
- RestTemplate 请求url
1.get 请求 RestTemplate restTemplate = new RestTemplate(); String url = ""; JSONObject resul ...
随机推荐
- nfs高可用
一.简介 NFS是单点的,如果一个节点出现问题,那使用它挂载服务的都将出现问题.所以需要高可用,挂掉一台不影响.采用keepalived+rsync+inotify-tools 环境: ubunt ...
- SSM框架新特性关于用Java配置类完全代替XML
项目目录结构 从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法, 这些方法将会被AnnotationConf ...
- [LeetCode] 79. Word Search 单词搜索
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- [LeetCode] 291. Word Pattern II 词语模式 II
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- [LeetCode] 312. Burst Balloons 爆气球
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- [LeetCode] 529. Minesweeper 扫雷
Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representin ...
- linux中安装robot环境
https://www.cnblogs.com/lgqboke/p/8252488.html(文中验证robotframework命令应该为 robot --version) 可能遇到的问题: 1.p ...
- UE4的内存模型
转自:https://blog.csdn.net/noahzuo/article/details/73565259 UObject和FUObjectItem UE4运行的基本单位是UObjet,然而U ...
- 【神经网络与深度学习】Win10+VS2015 caffe环境搭建(极其详细)
caffe是好用,可是配置其环境实在是太痛苦了,依赖的库很多不说,在VS上编译还各种报错,你能想象那种被一百多个红色提示所笼罩的恐惧. 且网上很多教程是VS2013环境下编译的,问人很多也说让我把1 ...
- 【VS开发】MFC动态设置对话框属性 弹出或嵌入
参考: MFC的对话框CDialog是怎么控制窗口可调整大小的属性的呢?打开资源文件,对话框资源的属性列表中,有一个"Border"项,改变该项的值就可以改变窗口边框风格.实际上w ...