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 请求的更多相关文章

  1. SpringMVC RestTemplate的几种请求调用

    转载:https://blog.csdn.net/ThinkingLink/article/details/45366777 1.用统一的方法模板进行四种请求:POST,PUT,DELETE,GET ...

  2. RestTemplate进行表单请求,注意要使用MultiValueMap

    在对接API的时候,有时候文档中会说,表单提交,这时候就需要用到 MultiValueMap来操作,下面给大家展示一个简单的demo. MultiValueMap<Object, Object& ...

  3. 精讲RestTemplate第8篇-请求失败自动重试机制

    本文是精讲RestTemplate第8篇,前篇的blog访问地址如下: 精讲RestTemplate第1篇-在Spring或非Spring环境下如何使用 精讲RestTemplate第2篇-多种底层H ...

  4. RestTemplate远程调用POST请求:HTTP 415 Unsupported Media Type

    这是本项目的接口 称为client @POST @Path("/{urlcode}") @Consumes(MediaTypes.JSON_UTF_8) @Produces(Med ...

  5. restTemplate 发送http post请求带有文件流、参数

    String httpMethod = ""; RestTemplate restTemplate = new RestTemplate(); String args = &quo ...

  6. RestTemplate的三种请求方式

    转载 https://blog.csdn.net/qq_36364521/article/details/84203133

  7. Spring RestTemplate中几种常见的请求方式

    https://github.com/lenve/SimpleSpringCloud/tree/master/RestTemplate在Spring Cloud中服务的发现与消费一文中,当我们从服务消 ...

  8. RestTemplate发送HTTP、HTTPS请求

    RestTemplate 使用总结   场景: 认证服务器需要有个 http client 把前端发来的请求转发到 backend service, 然后把 backend service 的结果再返 ...

  9. Spring RestTemplate中几种常见的请求方式GET请求 POST请求 PUT请求 DELETE请求

    Spring RestTemplate中几种常见的请求方式 原文地址: https://blog.csdn.net/u012702547/article/details/77917939   版权声明 ...

随机推荐

  1. Oracle EBS OM 取消订单行

    DECLARE l_header_rec OE_ORDER_PUB.Header_Rec_Type; l_line_tbl OE_ORDER_PUB.Line_Tbl_Type; l_action_r ...

  2. MySQL中的xtrabackup的原理解析

    xtrabackup的官方下载地址为 http://www.percona.com/software/percona-xtrabackup. xtrabackup包含两个主要的工具,即xtraback ...

  3. Zabbix添加Ping外网IP监控

    1.先添加一个HOST,只填写IP即可,如10.1.1.1 2.选择一台客户端,新建item,如下: 3.新建trigger: 注:icmpping[<target>,<packet ...

  4. LabVIEW TCP/IP 断开重连问题

    LabVIEW的TCP/IP函数库非常好用,但是不恰当地设置打开连接结点的参数将带来一些问题,麻烦.如下图的打开连接的参数设置: 上图中指定了本地的端口,会发生这样的情况.当我们关闭应用程序之后,连接 ...

  5. MongoDB基础之 用户和数据库基于角色的访问控制

    mongod 关键字参数:--auth 默认值是不需要验证,即 --noauth,该参数启用用户访问权限控制:当mongod 使用该参数启动时,MongoDB会验证客户端连接的账户和密码,以确定其是否 ...

  6. 跨平台开发 -- C# 使用 C/C++ 生成的动态链接库

    操作环境:Visual Studio 2017 如何实现 使用 C# 进行嵌入式开发? .NET Core 虽然实现了跨平台,但是不可能处处使用 C# 开发,就好像没人使用SQL开发安卓APP,每种语 ...

  7. 2.2Python基础语法(二)之运算符

    返回总目录 目录: 1.Python运算符的分类 2.算数运算符 3.复合运算符 4.比较运算符 5.逻辑运算符 (一)Python运算符的分类: (二)算数运算符: 注意下面三种算数符号: 1.** ...

  8. [LOJ 6031]「雅礼集训 2017 Day1」字符串

    [LOJ 6031] 「雅礼集训 2017 Day1」字符串 题意 给定一个长度为 \(n\) 的字符串 \(s\), \(m\) 对 \((l_i,r_i)\), 回答 \(q\) 个询问. 每个询 ...

  9. priority_queue的优先级变化(结构体的写法)

    priority_queue的优先级变化(结构体的写法) 在头文件中加上#include <queue> 即可使用stl中的库函数priority_queue,优先队列默认的是从大到小的优 ...

  10. SDN2017 第二次实验作业

    安装floodlight 参考链接:http://www.sdnlab.com/19189.html 从github下载源码,并编译安装 $ sudo apt-get install build-es ...