使用 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 版权声明 ...
随机推荐
- FAST特征点检测算法
一 原始方法 简介 在局部特征点检测快速发展的时候,人们对于特征的认识也越来越深入,近几年来许多学者提出了许许多多的特征检测算法及其改进算法,在众多的特征提取算法中,不乏涌现出佼佼者. 从最早期的Mo ...
- windows 服务器MYSQL 数据库安装配置
一.到官网下载MYSQL 打开官网地址:www.mysql.com, 选择 DOWNLOADS,进入到MySQL的下载页面,在页面的底部有一个MySQL Community Edition, 并且下面 ...
- SQL Server在本地计算机上用SSMS(SQL Server Management Studio)登录不上,错误消息:(Microsoft SQL Server, Error: 18456)
今天遇到了一个奇怪的问题,公司目前在SQL Server上都采用AD域账号登录,由于账号人数众多,所以我们建立了一个AD Group(域组),将大家的AD账号加入了这个AD Group,然后我们将这个 ...
- 学习笔记:ALTERing a Huge MySQL Table - 对一个超大表做alter调整
Table of Contents The ProblemFuture SolutionsOverview of SolutionShortcutAssumptions/Restrictions/Co ...
- MySQL锁系列之锁的种类和概念
转自:http://keithlan.github.io/2017/06/05/innodb_locks_1/ 锁是MySQL里面最难理解的知识,但是又无处不在. 一开始接触锁的时候,感觉被各种锁类型 ...
- cisco查看机框 板卡 电源 SN 风扇环境运行状态和一些常用命令 巡检命令
查看设备运行环境及状态 show environment 查看设备环境show environment temperature --查设备温度 show environment fans --查看设备 ...
- Gmail 设置,时区
问题提出: 我们工作的时候,需要和不同时区的人进行合作.我们需要注意时区问题.如果没有设置好时区,会造成很多不便. 了解时区问题: 通过 这个网站可以,让你对时区有所了解:http://zh.thet ...
- Linux nmap命令详解
nmap,也就是Network Mapper,是Linux下的网络扫描和嗅探工具包. nmap是在网络安全渗透测试中经常会用到的强大的扫描器.功能之强大,不言而喻.下面介绍一下它的几种扫描命令.具体的 ...
- Excel思考问题的方式
Excel思考问题的方式 一.写需求,说我要什么数据 好比如,现在咱们需要将第一周.第二周.第三周.第四周.….等E:E列里的"每一周的 第二个数值"提取出来.那么我们手动提取了几 ...
- 彻底理解lib和dll
转自:http://www.cppblog.com/amazon/archive/2009/09/04/95318.html 两种库:一种是LIB包含了函数所在的DLL文件和文件中函数位置的信息(入口 ...