使用 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 版权声明 ...
随机推荐
- 【转】C++11的enum class & enum struct和enum
转自:https://blog.csdn.net/sanoseiichirou/article/details/50180533 C++标准文档——n2347(学习笔记) 链接:http://www. ...
- 《细说PHP》第二版--读书笔记
第五章 PHP的基本语法 5.2.4 在程序中使用空白的处理 5.3 变量 5.3.1 变量的声明 在php中变量的声明必须是使用一个$符号,后面跟变量名来表示 unset()函数释放指定变量 iss ...
- jqGrid删除单条和多条数据
$("#Delete").click(function () { //var id = $("#showGrid").jqGrid('getGridParam' ...
- SQL Server下ADO.NET 怎么获取数据库SQL语句INSERT,UPDATE,DELETE了多少行数据
ADO.NET 在发送SQL语句到SQL Server数据库后,怎么知道真正INSERT,UPDATE,DELETE了多少行数据呢? 使用SQL Server内置的全局变量@@ROWCOUNT即可,@ ...
- ping 命令 指定特定网卡 发送 ICMP 数据包
Windows : ping -S Linux : ping -I <device> -I interface interface is either an address, or an ...
- MySQL基础之 索引
MySQL索引讲解 索引的好处: MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度. 打个比方,如果合理的设计且使用索引的MySQL是一辆兰博基尼的话,那么 ...
- TiDB数据库 mydumper与loader导入数据
从mysql导出数据最好的方法是使用tidb官方的工具mydumper. 导入tidb最好的方法是使用loader工具,大概19.4G每小时的速度. 详细的步骤可以参考官网:https://pingc ...
- WINDOWS SOCKET编程中accept出来的新连接是阻塞还是非阻塞
实践证明 SOCKET hNewSock=accept(hListenSock) 当hListenSock为阻塞模型时,hNewSock则为阻塞模型 否则 当hListenSock为非阻塞模型时,hN ...
- jQuery事件整合
一.jQuery事件 1.focus()元素获得焦点 2.blur()元素失去焦点 3.change() 表单元素的值发生变化(可用于验证用户名是否存在) 4.click() 鼠标单击 5.dbcli ...
- 【转】Python操作MongoDB数据库
前言 MongoDB GUI 工具 PyMongo(同步) Motor(异步) 后记 前言 最近这几天准备介绍一下 Python 与三大数据库的使用,这是第一篇,首先来介绍 MongoDB 吧,,走起 ...