使用 restTemplate 实现get/post 请求
get 请求(这里是在 idea 的 test包中,所以需要直接 new RestTemplate() )
即:RestTemplate restTemplate = new RestTemplate();
package org.darkblue.monitor; import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate; public class TestTemplate { @Test
public void test() {
RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders();
MultiValueMap<String, String> postParameters = new LinkedMultiValueMap<String, String>();
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(postParameters, headers);
String str = restTemplate.postForObject("http://192.168.1.15/DETECT-X/", requestEntity, String.class); System.out.println(str.toString()); } }
post 请求(这里是在非test包中,所以可以自动注入):
package org.darkblue.monitor.controller; import com.kinome.surveillance.web.dao.RepoDao;
import com.kinome.surveillance.web.dao.RepoDaoImp;
import com.kinome.surveillance.web.entity.RepoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate; import javax.servlet.http.HttpServletRequest;
import java.util.List; @Controller
public class CensorStatusController { @Autowired
HttpServletRequest request;
@Autowired
RestTemplate restTemplate; @ResponseBody
@RequestMapping("updateCensorStatus")
public String updateCensorStatus(RepoEntity re) { RepoDao repoDao = new RepoDaoImp();
RepoEntity repoEntity = new RepoEntity();
int intCensorStatus = re.getCensorStatus();
int intRid = re.getRid();
System.out.println("intRid =========> " + intRid);
List<RepoEntity> repoByRid = repoDao.getRepoByRid(intRid);
for (RepoEntity rbr :
repoByRid) {
repoEntity.setPtLink(rbr.getPtLink());
repoEntity.setRemark(rbr.getRemark());
} if (re.getCensorStatus() == 2) {
repoDao.deleteRid(intRid); int userId = (int) request.getSession().getAttribute("userId");
HttpHeaders headers = new HttpHeaders();
headers.add("X-Auth-Token", "e348bc22-5efa-4299-9142-529f07a18ac9"); MultiValueMap<String, String> postParameters = new LinkedMultiValueMap<String, String>();
postParameters.add("item0_1", repoEntity.getPtLink());
postParameters.add("item0_2", repoEntity.getRemark());
postParameters.add("userid", String.valueOf(userId)); HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(postParameters, headers); String str = restTemplate.postForObject("http://localhost:8080/DETECT-X/BulkAddPtLinkAndRemark.action", requestEntity, String.class);
// System.out.println(str.toString());
return "1";
} else {
if (repoDao.setCensorStatusByRid(intCensorStatus, intRid)) {
return "1";
} else {
return "0";
}
} } }
使用 restTemplate 实现get/post 请求的更多相关文章
- SpringMVC RestTemplate的几种请求调用
转载:https://blog.csdn.net/ThinkingLink/article/details/45366777 1.用统一的方法模板进行四种请求:POST,PUT,DELETE,GET ...
- RestTemplate进行表单请求,注意要使用MultiValueMap
在对接API的时候,有时候文档中会说,表单提交,这时候就需要用到 MultiValueMap来操作,下面给大家展示一个简单的demo. MultiValueMap<Object, Object& ...
- 精讲RestTemplate第8篇-请求失败自动重试机制
本文是精讲RestTemplate第8篇,前篇的blog访问地址如下: 精讲RestTemplate第1篇-在Spring或非Spring环境下如何使用 精讲RestTemplate第2篇-多种底层H ...
- RestTemplate远程调用POST请求:HTTP 415 Unsupported Media Type
这是本项目的接口 称为client @POST @Path("/{urlcode}") @Consumes(MediaTypes.JSON_UTF_8) @Produces(Med ...
- restTemplate 发送http post请求带有文件流、参数
String httpMethod = ""; RestTemplate restTemplate = new RestTemplate(); String args = &quo ...
- RestTemplate的三种请求方式
转载 https://blog.csdn.net/qq_36364521/article/details/84203133
- Spring RestTemplate中几种常见的请求方式
https://github.com/lenve/SimpleSpringCloud/tree/master/RestTemplate在Spring Cloud中服务的发现与消费一文中,当我们从服务消 ...
- RestTemplate发送HTTP、HTTPS请求
RestTemplate 使用总结 场景: 认证服务器需要有个 http client 把前端发来的请求转发到 backend service, 然后把 backend service 的结果再返 ...
- Spring RestTemplate中几种常见的请求方式GET请求 POST请求 PUT请求 DELETE请求
Spring RestTemplate中几种常见的请求方式 原文地址: https://blog.csdn.net/u012702547/article/details/77917939 版权声明 ...
随机推荐
- Ubuntu安装ffmpeg
二.linux源码安装: 如下: 下载 源码包:http://ffmpeg.org/releases/ffmpeg-3.3.tar.bz2 1.从网络上下载到的源码包,然后解压到指目录 假设下载目录 ...
- leveldb源码分析--BloomFilter
bloomfilter是leveldb中的一大性能利器,所以为了文章的表现完整性这里新启这么一篇文章.leveldb中的bloomfilter的实现在bloom.cc中,是一个较为简单的实现,所以就不 ...
- python selenium模拟登录163邮箱和QQ空间
最近在看python网络爬虫,于是我想自己写一个邮箱和QQ空间的自动登录的小程序, 下面以登录163邮箱和QQ空间和为例: 了解到在Web应用中经常会遇到frame/iframe 表单嵌套页面的应用, ...
- Ansible自动化运维工具使用
概述本文描述自动化运维工具 Ansible 的安装及基础使用方法,包含: Centos 下的安装主机配置Ad-Hoc command(命令行执行)Playbook (任务剧本)Ansible 和 Sa ...
- 按ctrl+shift切换不了输入法
解决方法: 1.在任务栏上的输入法图标上点右键选择设置2.选择键设置,双击第一个“在不同的输入语言之间切换”先勾选“切换输入语言”下面选择左手ALT.取消右边“切换键盘布局”前的勾3.进入“中文(简体 ...
- 第 14 章 结构和其他数据形式(names3)
/*----------------------------------- names3.c -- 使用指针和 malloc() ----------------------------------- ...
- [Python]运算符的优先级顺序
运算符 描述 ** 指数 (最高优先级) ~ + - 按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@) * / % // 乘,除,取模和取整除 + - 加法减法 >> & ...
- 关于Java集合类库中的几种常用队列
Java中几种常用的队列 阻塞队列与普通队列的区别在于,当队列是空的时,从队列中获取元素的操作将会被阻塞,或者当队列是满时,往队列里添加元素的操作会被阻塞.试图从空的阻塞队列中获取元素的线程将会被阻塞 ...
- HTML5中的Canvas详解
什么是Canvas HTML5 <canvas> 元素用于图形的绘制,通过脚本 (通常是JavaScript)来完成.<canvas> 标签只是图形容器,您必须使用脚本来绘制图 ...
- VS2013自带报表+打印功能
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u010773667/article/details/27540389 经过了VB版机房收费系统的学习 ...